Pergunta

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>
Foi útil?

Solução 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]

Outras dicas

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

I think what you are looking for is:

results[1].geometry.location

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);
}});

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top