Bing 地图:如何根据搜索查询设置最佳缩放比例

Bing Maps: how to set best zoom depending on search query

如何根据使用 ajax API v7 的用户搜索查询在 Bing 地图上设置最佳缩放级别。例如,如果搜索查询是一个城市名称——我需要设置缩放级别来显示整个城市,如果查询是一个准确的地址——我需要显示这个地址,所以缩放级别会更大。就像它在这里工作一样:https://www.bing.com/maps/

Microsoft.Maps.LocationRect.fromLocations 没有帮助。它只使地图居中。

您可以通过设置 ViewOptions.bounds via Microsoft.Maps.Map.setView method:

来实现

bounds LocationRect The bounding rectangle of the map view. If both are specified, bounds takes precedence over center.

例子

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>