2016-10-11 34 views
1

整體畫面:Bing地圖地理編碼回調則不會調用

我想地理編碼添加到我的應用程序。我能夠直接使用JavaScript,但在轉換爲Angular/TypeScript後,回調沒有被觸發。

實施例:如果用戶輸入1 Microsoft方法,華盛頓州雷蒙德市。經度和緯度應返回:緯度:47.64006815850735,經度:-122.12985791265965

的代碼示例內置折以下資源:

錯誤詳細信息:

錯誤發生在變量名稱中:geocodeRequestsearchModuleLoaded()被加載,但我的geocodeRequest從不觸發geocodeCallbackerrCallback。我認爲它與我的方法的範圍有關,但似乎無法隔離導致錯誤的原因。關於如何讓我的回調觸發的任何想法?

角/打字稿(不工作)

$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."); 
} 

回答

0

furthing調查後,我能解決我的問題。

問題是什麼?

該問題發生在searchModuleLoaded內。這兩個回調都是undefined。一個問題是它在模塊加載之前試圖執行searchModuleLoaded,另一個問題是由於它不知道this的上下文而引起的。

要解決這個問題,我不得不在修改回調時加載Microsoft.Maps.Search。該模塊的回調現在轉換爲一個lambda函數,該函數調用this.searchModuleLoaded()。一旦將其編譯爲JavaScript,它將適當地設置this上下文,即_this = this。我的代碼現在看起來像這樣:

getMap() { 
    this.map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: "put 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) { 
    alert("The first geocode result is " + geocodeResult.results[0].location + "."); 
}; 

errCallback(geocodeRequest) { 
    alert("An error occurred."); 
};