In my angular 9 application i am using @agm/core
to display map and showing a marker on the map and maker is draggable but i want marker should fixed on center and map should be moveable when map move stoped then update lat and lng how can i do this in angular. thanks in advance.
this is my component
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);
});
}
}
this is my view code
<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>