0

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

My code is this:

<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>
Luca Angioloni
  • 2,243
  • 2
  • 19
  • 28
user1987095
  • 439
  • 2
  • 8
  • 18

5 Answers5

3

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
Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
2

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]
Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
  • 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); – user1987095 Dec 23 '13 at 14:23
  • 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? – Anto Jurković Dec 23 '13 at 18:58
1

I think what you are looking for is:

results[1].geometry.location
Rami Sakr
  • 372
  • 1
  • 14
1

Try to pass your lat and long to the following query string, and you will get a json array, fetch your city from there

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

NOTE: you have to pass your api key into queryParams, that you can generate from google Clouds

Zaman Rajpoot
  • 326
  • 2
  • 5
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
1

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);
}});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31