0
該問題發生在頁面刷新期間,然後瀏覽器被最小化或者其帶有地圖的選項卡處於非活動狀態。然後,搜索管理器地理編碼功能將落入errorCallback。如果帶有地圖的頁面處於活動狀態(可見),則一切正常。微軟必應地圖v7搜索管理器地理編碼errorCallBack
我檢查了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>
其實,逗號是在我原來的代碼中。我剛剛在這裏錯過了它,同時試圖壓縮和格式化StackOverflow的代碼。就像我在我的評論中提到的那樣 - 如果地圖的頁面(標籤)可見,則一切正常。只有在瀏覽器最小化或者地圖選項卡處於非活動狀態時纔會失敗。我確實在v8中嘗試了這個,在創建這篇文章之後,通過創建一個簡單的地圖並使用示例數據 - 結果相同。我最終編寫了自定義(而且不幸地是速度較慢)的地理編碼服務,將其用作搜索管理器中的錯誤回退。 –
我也想知道爲什麼搜索管理器在其構造函數中需要一個地圖,如果地理編碼查詢和結果獨立於它(地圖)。我還嘗試將「邊界」屬性填充到地理編碼搜索請求中,認爲它們無法正確計算,但又是 - 沒有運氣。 –
搜索管理器使用地圖視圖幫助影響/加權結果。它還從地圖抓取會話密鑰。如果你不關心這個,你可以直接使用REST location API:http://bingmapsv8samples.azurewebsites.net/#RestServices_JSONP – rbrundritt