我正在尝试平移一些经纬度,同时显示上面的错误。 但它显示了以上错误。
component.ts
public map: any;
zoom;
lat;
currentLoc: boolean;
lng;
constructor() {
this.zoom = 12;
this.lat = 12.972442;
this.lng = 77.580643;
}
ngOnInit() {
this.setCurrentPosition();
}
private setCurrentPosition() {
console.log('setting');
this.currentLoc = true;
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
this.zoom = 12;
});
}
}
public mapReady(map) {
this.map = map;
}`
drawOnMap() {
for (let i = 0; i < this.Locations.length; i++) {
// console.log(this.Locations[i].latitude, this.Locations[i].longitude);
const coordinate = new Coordinate(this.Locations[i].latitude, this.Locations[i].longitude);
// console.log( 'Coordinate is :' + coordinate);
this.coordinates.push(coordinate);
this.map.panTo({ lat: this.Locations[0].latitude, lng: this.Locations[0].longitude });
}
}
this.Locations []有一些经度和经度,它不为空
HTML
<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom" [styles]="styles" (mapReady)="mapReady($event)">
<agm-marker *ngFor="let marker of coordinates; let i = index" [iconUrl]="station" [latitude]="marker.latitude" [longitude]="marker.longitude"
[markerClickable]="true" (markerClick)="markerClicked(marker.latitude,marker.longitude)">
</agm-marker>
<agm-marker *ngIf="currentLoc === true" [iconUrl]="Liveicon" [latitude]="lat" [longitude]="lng">
</agm-marker>
<agm-map>
请纠正我错了的地方。我无法得到这个。
答案 0 :(得分:0)
问题很简单,地图根本不会实例化。
在html中,mapRead()方法正在侦听自己的声音以启动地图,如果您不从外部调用它,那将永远不会发生。
解决方案很简单。只需在构造函数中或在ngOnInit上实例化地图,就像这样:
...
ngOnInit() {
this.map = map;
}
...
在这种情况下,mapRead方法是多余的。除非您想动态实例化地图。
答案 1 :(得分:0)
实际上,不需要整个setCurrentPosition方法,幸运的是,角度google map包为您做了很多事情,因此您需要做的就是更改map.lat来更改地图的位置(和焦点)。还有this.lng。