【发布时间】:2020-09-26 16:27:38
【问题描述】:
在我的 Angular 9 应用程序中,我使用 @agm/core 来显示地图并在地图上显示标记,并且制造商是可拖动的,但我希望标记应该固定在中心,并且当地图移动停止时地图应该是可移动的,然后更新 lat 和 lng我怎么能以角度做到这一点。提前致谢。
这是我的组件
constructor(private mapsAPILoader: MapsAPILoader,
private ngZone: NgZone) {
}
ngOnInit() {
//load Places Autocomplete
this.mapsAPILoader.load().then(() => {
this.setCurrentLocation();
this.geoCoder = new google.maps.Geocoder;
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement);
autocomplete.addListener("place_changed", () => {
this.ngZone.run(() => {
//get the place result
let place: google.maps.places.PlaceResult = autocomplete.getPlace();
//verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
//set latitude, longitude and zoom
this.latitude = place.geometry.location.lat();
this.longitude = place.geometry.location.lng();
this.zoom = 12;
});
});
});
}
// Get Current Location Coordinates
private setCurrentLocation() {
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
this.zoom = 8;
this.getAddress(this.latitude, this.longitude);
});
}
}
这是我的视图代码
<agm-map #gm [latitude]="latitude" [longitude]="longitude" [zoom]="zoom">
<agm-marker [latitude]="latitude" [longitude]="longitude" [markerDraggable]="true"
(dragEnd)="markerDragEnd($event)" (mouseOver)="onMouseOver(infoWindow,gm)"
(mouseOut)="onMouseOut(infoWindow, $event)">
<agm-info-window [isOpen]="false" #infoWindow>
<a class="btn btn-attention pull-right">
Press the Red Location Pin, then move the Pin to your location。
<i class="fa fa-angle-double-right"></i>
</a>
</agm-info-window>
</agm-marker>
</agm-map>
【问题讨论】:
标签: javascript angular