我正在建立一個基於谷歌地圖API Vue(版本2.x)的組件。使功能等待結果在javascript
在這個API中有一個嵌入函數來獲取地圖的邊界:getBounds()(ref)。
在我的代碼,我初始化與邊框對象可在地圖等於0:
currentBorders: {
b: {
b: 0,
f: 0,
},
f: {
b: 0,
f: 0,
},
},
在那之後,我顯示具有一定中心的地圖,我執行的getBounds()函數來獲得當前地圖上的價值已經集中後:
getBorders() {
console.log(this.currentBorders); // -> prints the object with all values equal to 0
this.currentBorders = this.$map.getBounds();
console.log(this.currentBorders); // -> prints undefined
return {
llc_lat: this.currentBorders.f.b,
llc_lng: this.currentBorders.b.b,
urc_lat: this.currentBorders.f.f,
urc_lng: this.currentBorders.b.f,
};
},
現在的問題是,第一個執行過程中我必須設置爲0 currentBorders,但的getBounds第一次執行()返回不確定,可能是因爲地圖尚未加載。
我想要做的就是阻止代碼的執行,直到getBounds返回一些有意義的東西。這是要走的路嗎?我怎樣才能做到這一點?
編輯: 這是我如何初始化地圖,通過deferredReady:
deferredReady() {
this.$map.setOptions({
styles: mapStyles(),
});
this.initializeMapOverlay();
this.canvasContext = this.mapCanvas.getContext("2d");
this.$map.addListener("idle", this.idleHandler);
this.$map.addListener("zoom_changed",() => {
this.resizeCanvas();
this.clearCanvas();
});
this.$map.addListener("mousemove", this.mouseoverHandler);
this.$map.addListener("drag", this.fetchHexagons());
},
你能告訴我們關於你的地圖實例和你getBounrds()調用的代碼嗎? –
爲什麼不等待地圖初始化之後再調用'getBounds'?或者是你的問題? – thanksd