JustFly1984 / react-google-maps-api

React Google Maps API
MIT License
1.78k stars 437 forks source link

How to get address from Marker onDragEnd? #237

Closed subhendukundu closed 3 years ago

subhendukundu commented 4 years ago

Hey there,

First of all, thanks for so easy to use the library. I am trying to get the location address of the marked dropped or dragged to. How do I get the location of the marker dragged to? I figured to get the lat and lng using markerRef.current.position.lat(), markerRef.current.position.lng(). Is there a better way to get the location? With the address?

I could use react-geocode or something to get the address but I wanted to know if this library helps me with that too?

Node: 10, react: 16.9.0, @react-google-maps/api: 1.7.7

Sandbox: https://codesandbox.io/embed/react-google-maps-api-get-marker-location-7hotz

Thanks for the help in advance

dogofpavlov commented 4 years ago

I don't believe this library has anything for geocoding provided in it. That being said, the reason is probably because google's suggested way to do it is just using their API: https://developers.google.com/maps/documentation/geocoding/intro#geocoding. The library you linked to seems to do the same.

JustFly1984 commented 4 years ago

exactly. If you have places loaded with <LoadScript />, google object is available to you, and you can use geocoding API https://developers.google.com/maps/documentation/geocoding/intro#geocoding

hipdev commented 3 years ago

@subhendukundu You can just use:

const handleNewLocation = (marker) => {
  console.log(marker.latLng.lat(), marker.latLng.lng(),  'lat and lng from marker')
}
...
<Marker 
...
onDragEnd={handleNewLocation}
/>