【发布时间】:2019-07-21 09:40:37
【问题描述】:
我的数据库中有一个地理位置列表,并将它们存储在一个变量中,如下所示:
.ts
ngOnInit() {
this.getListings()
}
getListings() {
this.serviceHomeScreen.getAllListingsDetails().subscribe((data) => {
this.extractlistings(data[0])
})
}
extractlistings(data) {
this.locations = data
console.log(this.locations[1]) // has the right value
}
mapReady(map) {
console.log(this.locations[1]) // undefined
for (let i = 0; i < this.locations.length; i++) {
console.log("aici")
new google.maps.Marker({
position: { lat: this.locations[i].lat, lng: this.locations[i].long },
map: map,
});
}
}
.html
<agm-map id="map_" [zoom]="30" [fitBounds]="true" [latitude]="lat" [longitude]="lng" [streetViewControl]="false" [zoomControl]="false" (mapReady)="mapReady($event)"></agm-map>
位置是这样的
locations = [{'id': 1, 'lat': '43', long: '-12'}, {'id': 2, 'lat': '34', long: '-15'}, {'id': 3, 'lat': '13', long: '-12'}]
问题是我从服务中得到了这个locations 数组,它有正确的值,但是在mapReady() 函数内部,当我需要数组中的纬度和经度时,是undefined。
这是为什么呢? mapReady 在 ngOnInit 之前被调用?
感谢您的宝贵时间!
【问题讨论】:
标签: html angular google-maps angular6