gpt4 book ai didi

javascript - 必应 map : how to set best zoom depending on search query

转载 作者:行者123 更新时间:2023-11-28 00:14:58 24 4
gpt4 key购买 nike

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

Microsoft.Maps.LocationRect.fromLocations 没有帮助。它仅使 map 居中。

最佳答案

您可以通过设置ViewOptions.bounds来实现它通过 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>

关于javascript - 必应 map : how to set best zoom depending on search query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30543952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com