整體畫面:Bing地圖地理編碼回調則不會調用
我想地理編碼添加到我的應用程序。我能夠直接使用JavaScript,但在轉換爲Angular/TypeScript後,回調沒有被觸發。
實施例:如果用戶輸入1 Microsoft方法,華盛頓州雷蒙德市。經度和緯度應返回:緯度:47.64006815850735,經度:-122.12985791265965
的代碼示例內置折以下資源:
- Bing Maps Ajax API - get location from address
- https://msdn.microsoft.com/en-us/library/hh868062.aspx
錯誤詳細信息:
錯誤發生在變量名稱中:geocodeRequest
。 searchModuleLoaded()
被加載,但我的geocodeRequest
從不觸發geocodeCallback
或errCallback
。我認爲它與我的方法的範圍有關,但似乎無法隔離導致錯誤的原因。關於如何讓我的回調觸發的任何想法?
角/打字稿(不工作)
$onInit() {
this.getMap();
}
getMap() {
this.map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: "your key here"});
Microsoft.Maps.loadModule('Microsoft.Maps.Search', { callback: this.searchModuleLoaded });
};
searchModuleLoaded() {
var searchManager = new Microsoft.Maps.Search.SearchManager(this.map);
var geocodeRequest = {
where: "1 Microsoft Way, Redmond, WA",
count: 10,
callback: this.geocodeCallback,
errorCallback: this.errCallback
};
searchManager.geocode(geocodeRequest);
}
geocodeCallback(geocodeResult, userData) {
// this callback never gets triggered
alert("The first geocode result is " + geocodeResult.results[0].location + ".");
}
errCallback(geocodeRequest) {
// this callback never gets triggered
alert("An error occurred.");
}
工作版本(工程,但沒有角/打字稿)
function GetMap(){
map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {credentials: "key goes here", center: new Microsoft.Maps.Location(47.5, -122.3), zoom: 9 });
Microsoft.Maps.loadModule('Microsoft.Maps.Search', { callback: searchModuleLoaded });
}
function searchModuleLoaded(){
var searchManager = new Microsoft.Maps.Search.SearchManager(map);
var geocodeRequest = {where:"1 Microsoft Way, Redmond, WA", count:10, callback:geocodeCallback, errorCallback:errCallback};
searchManager.geocode(geocodeRequest);
debugger;
}
function geocodeCallback(geocodeResult, userData){
alert("The first geocode result is " + geocodeResult.results[0].location + ".");
}
function errCallback(geocodeRequest){
alert("An error occurred.");
}