I've implemented AGM (Angular Google Maps) in my Ionic Project to showcase Google Maps, and I'm looking to have the marker stay centered on the map even when it is dragged. Is there a way to achieve this with AGM? Please let me know if I have made any errors. Thank you.
Below is a snippet of my current implementation. I have utilized the dragend
method as per the guidelines provided in the AGM Docs.
map.component.html
<agm-map
#agmMap
[style.height.px]="height"
[latitude]="lat"
[longitude]="lng"
[zoom]="zoom"
[usePanning]="true"
(mapReady)="mapReady($event)"
>
<agm-marker
[iconUrl]="{
url:
'https://cdn0.iconfinder.com/data/icons/small-n-flat/24/678111-map-marker-512.png',
anchor: { x: 14.5, y: 28 },
scaledSize: {
width: 30,
height: 30
}
}"
[latitude]="lat"
[longitude]="lng"
[markerDraggable]="true"
(dragEnd)="markerDragEnd($event)"
></agm-marker>
</agm-map>
map.component.ts
setNewCenter(latitude, longitude) {
this.lat = latitude;
this.lng = longitude;
this.getAddress(this.lat, this.lng);
}
public mapReady(map) {
map.addListener("dragend", () => {
this.setNewCenter(map.getCenter().lat(), map.getCenter().lng());
});
}
Here's how the map looks after using dragend
, you can view it here ->
https://ibb.co/9qVcLN5
Alternatively, when utilizing drag
instead of dragend
, the outcome is like the following preview.
https://i.sstatic.net/Ybdpu.gif