Microsoft 必应地图 v7 搜索管理器地理编码错误回调



问题发生在页面刷新期间,然后浏览器被最小化或其带有地图的选项卡处于非活动状态。然后搜索管理器地理编码函数陷入错误回调。如果带有地图的页面处于活动状态(可见(,则一切正常。

我在 errorCallback 函数中检查了 e.request 对象,它包含正确的"where"参数,但没有纬度和经度,也没有关于错误的任何信息。

该问题可以在Chrome和IE浏览器中重现。

.HTML:<div id="map" class="map" style="height:270px; width:100%"></div>

JavaScript:

<script type="text/javascript" src="https://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&s=1"></script>
<script type="text/javascript">
// global variables
var apiKey = 'API_KEY_HIDDEN',
map,
searchManager;
// sample data
var siteData = [
{"Name":"Starbucks","Address":"8400 SW Nimbus Ave 120","City":"Beaverton","State":"OR","Zip":"97008","Latitude":0,"Longitude":0},
{"Name":"Subway","Address":"12160 SW Scholls Ferry Rd","City":"Tigard","State":"OR","Zip":"97223","Latitude":0,"Longitude":0}
];
$(document).ready(function () {
GetMap();
setTimeout(function() { location.reload(); }, 60000);
});
function GetMap() {    
// initialize the map
map = new Microsoft.Maps.Map(document.getElementById('map'), {
credentials: apiKey,
mapTypeId: Microsoft.Maps.MapTypeId.road,
zoom: 1
});
// load search module
Microsoft.Maps.loadModule('Microsoft.Maps.Search', {
callback: function () {
searchManager = new Microsoft.Maps.Search.SearchManager(map);
$.each(siteData, function(index, clientSite) {
GeoCodeQuery(clientSite);
});
}
});
}
function GeoCodeQuery(clientSite) {
// set search parameters
var searchRequest = {
where: clientSite.Address + ', ' + clientSite.City + ', ' + clientSite.State + ' ' + clientSite.Zip,
callback: function (data) {
if (data && data.results && data.results.length > 0) {
clientSite.Latitude = data.results[0].location.latitude;
clientSite.Longitude = data.results[0].location.longitude;
}
else {
console.log('No results.');
}
},
errorCallback: function (e) {
console.log('Search error.');
}
};
// make the geocode request
searchManager.geocode(searchRequest);           
}
</script>

几个问题;

  • where 参数后缺少逗号。这将使搜索请求成为无效的 JSON 对象。修复此问题会导致对第一个地址进行正确地理编码。第二个是抛出错误,这可能是由于多种原因,最有可能的是下一点。
  • 必应地图 V7 控件已于 6 月停用,并将很快关闭。它的一些后端服务已经被关闭,因此会有问题。您应该使用一年多前取代V7的Bing Maps V8。您可以在此处找到迁移指南:https://social.technet.microsoft.com/wiki/contents/articles/34563.bing-maps-v7-to-v8-migration-guide.aspx

最新更新