0
votes

I have google map with search box , i want to access marker position with (lat , lng ) when i search specific place , i can access location exactly when i drag & drop marker by event.addListener

var marker = new google.maps.Marker({
    position: place.geometry.location,
    map: map,
    draggable:true,
    title:"Drag me!",
});


google.maps.event.addListener(marker, 'dragend', function(position:any){
    This.centerLat = position.latLng.lat().toFixed(3)
    This.centerLng = position.latLng.lng().toFixed(3)
});

but i want to get marker location when search is done without drag & drop

2

2 Answers

1
votes

You can obtain the coords of a marker using

pos =  yourMarker.getPosition(); 

the result is an object with lat and lng

lat  = pos.lat;
lng  = pos.lng;

https://developers.google.com/maps/documentation/javascript/reference/marker?hl=en#Marker

0
votes

I found this answer too , and it work with me correctly :

var marker = new google.maps.Marker({
    position: place.geometry.location,
    map: map,
    draggable:true,
    title:"Drag me!",
});

google.maps.event.addListener(marker, "map_changed", function() {
    this.centerLat = place.geometry.location.lat().toFixed(3)
    this.centerLng = place.geometry.location.lng().toFixed(3)
});