How do i get address by given lat/long

23,537

Solution 1

Address with street number is at index 0. Use

results[0].formatted_address

instead of

results[1].formatted_address

And you will get the address

For example, at index 0 you get: "36 Osborne Street, Wollongong ...", at index 1 you get "Wollongong Hospital Crown St, Wollongong ...".

You can get components of formatted_address using

results[0].address_components[0]
...
results[0].address_components[5]

Solution 2

Check these two references:

How to get address location from latitude and longitude in Google Map.?

Get location address from Latitude and Longitude in google maps

The answer of these will help you.

use this URL to get the address:

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

Solution 3

I think what you are looking for is:

results[1].geometry.location

Solution 4

Pass your latitude, longitude, and API key to the following API

$.get({ url: `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${longi}&sensor=false&key=${apikey}`, success(data) {
    console.log(data.results[0].formatted_address);
}});
Share:
23,537
user1987095
Author by

user1987095

Updated on July 06, 2022

Comments

  • user1987095
    user1987095 almost 2 years

    How do i get address by given lat/long not location.I have tried with "Reverse Geocoding" but it showing only location name.

    My code is

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
    var geocoder;
    var map;
    var marker;
    var marker2;
    
    function initialize() {
        geocoder = new google.maps.Geocoder();
        var
        latlng = new google.maps.LatLng(-34.397, 150.644);
        var mapOptions = {
            zoom: 5,
            center: latlng
        }
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        google.maps.event.addListener(map, 'click', function (event) {
            //alert(event.latLng);          
            geocoder.geocode({
                'latLng': event.latLng
            }, function (results, status) {
                if (status ==
                    google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        alert(results[1].formatted_address);
                    } else {
                        alert('No results found');
                    }
                } else {
                    alert('Geocoder failed due to: ' + status);
                }
            });
        }); <!--click event--> }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    <div id="map-canvas"></div>
    
    • PiLHA
      PiLHA over 10 years
    • geocodezip
      geocodezip over 10 years
      Why are you using the 2nd result (results[1])? Perhaps the first result (results[0]) might be what you are looking for...
    • user1987095
      user1987095 over 10 years
      Thank you, you are right, results[0] giving the right answer
  • user1987095
    user1987095 over 10 years
    I visited this post, but not understand how do i get this pragmatically.can you help me
  • Code Lღver
    Code Lღver over 10 years
    @user1987095 you can use the ajax of jquery simply to get the address.
  • user1987095
    user1987095 over 10 years
    Thanks for your help,"Get location address from Latitude and Longitude in google maps" help me
  • Code Lღver
    Code Lღver over 10 years
    @user1987095 my pleasure to help you.
  • user1987095
    user1987095 over 10 years
    Hi Anto, I want a info box instead of alert but problem is, when clicking any point it showing the content but when click new location the previous one not removed. var marker3 = new google.maps.Marker({ position: event.latLng, map: map, draggable: true }); infowindow.setContent(results[0].formatted_address); infowindow.open(map, marker3);
  • Anto Jurković
    Anto Jurković over 10 years
    What do you want to achieve: when you click on some location that marker is created and infowindow opened? And when you click on another location that merker is moved to that location and infowindow updated with new address?