-2

When I change pointer on the map, I want to get name of a place. I'm able to get lat and lng of a place on the map, but I want to get name of that place. For exampl lat: 43.669992353311635, lng: -79.4108552 is TORONTO. What is mistake in my code?

library: react-google-maps

  onDragEnd: ({ setDrag, setCenter, setUseMyLocation, onChangeMapCoords  }) => () => {
    const center = refs.map.getCenter();
    const placeName = refs.map.getPlaces();
    console.log("ACTION sp8 map center", placeName)
    // console.log("ACTION sp8 map center", { lat: center.lat(), lng: center.lng()})

    setUseMyLocation(false);
    setDrag(false);
    setCenter(({ lat: center.lat(), lng: center.lng() }));
    resetSearchBox();
  },
  • please use 3 backticks code block for code section. – Chandan Dec 28 '20 at 14:09
  • 2
    Duplicate of [Is it possible to get an address from coordinates using google maps?](https://stackoverflow.com/questions/10008949/is-it-possible-to-get-an-address-from-coordinates-using-google-maps) – MrUpsidown Feb 10 '21 at 08:12

2 Answers2

0

You can use this way

 val geocoder = Geocoder(requireContext(), Locale.getDefault())
  // parameter 1 is the number of results you want, a maximum of 5
 val addresses: List<Address> = geocoder.getFromLocation(lat, lng, 1)
-1

You can use the Geocoding service to reverse geocode the latLng you are getting onDragEnd. This way, you can get the name of the address from the reverse geocoding.

Here is a sample code and the code snippet of Geocoding services below:

  onDragEnd(e) {
    console.log(e.latLng);
    const geocoder = new google.maps.Geocoder();
    let loc = e.latLng;
    geocoder.geocode({ location: loc }, (results, status) => {
      if (status === "OK") {
        if (results[0]) {
          console.log(results[0].formatted_address);
        } else {
          window.alert("No results found");
        }
      } else {
        window.alert("Geocoder failed due to: " + status);
      }
    });
  }
Pagemag
  • 2,779
  • 1
  • 7
  • 17