如何使用Ajax API v7根据用户搜索查询在Bing Maps上设置最佳缩放级别。例如,如果搜索查询是城市名称-我需要设置缩放级别以显示整个城市,并且如果查询是一个准确的地址-我需要显示此地址,因此缩放级别将更大。就像它在这里工作一样:https://www.bing.com/maps/
Microsoft.Maps.LocationRect.fromLocations没有帮助。它只会使地图居中。
您可以通过Microsoft.Maps.Map.setView方法设置ViewOptions.bounds来实现:
bounds
LocationRect 地图视图的边界矩形。如果都 指定了边界,则边界优先于中心。
例子
function initMap() {
var addresses = [
"1 Microsoft Way, Redmond, WA",
"Helsinki, Finland",
"Australia"
];
var map = new Microsoft.Maps.Map(
document.getElementById("myMap"),
{
credentials: "YOUR-BING-KEY",
mapTypeId: Microsoft.Maps.MapTypeId.road
}
);
Microsoft.Maps.loadModule('Microsoft.Maps.Search', {
callback: function() {
searchLocation(map, addresses[1], //<- set address
function(geocodeResult, userData) {
map.setView({bounds:geocodeResult.results[0].bestView });
},
function(geocodeRequest) {
console.log('An error occured...');
});
}
});
}
function searchLocation(map, query, success, error) {
var searchManager = new Microsoft.Maps.Search.SearchManager(map);
var geocodeRequest = { where: query, count: 1, callback: success, errorCallback: error };
searchManager.geocode(geocodeRequest);
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0" type="text/javascript" charset="UTF-8"></script>
</head>
<body onload="initMap();">
<div id="myMap" style="position: relative; width: 600px; height: 450px;"></div>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句