Recover Google Maps Address by informing Long and Lat

Asked

Viewed 2,895 times

3

I have the latitude and longitude of a location in Google Maps, obtained from a marker and a function of its own, I would like to know if anyone has an idea or function to return me the full address of the site based on these two parameters.

  • What is a "Full Address" to you? The postal address corresponding to that address? The link to the Google Maps page? Something else?

2 answers

4


Use the following URL to get a JSON with the address:

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

In this case, the latitude is: 44.4647452 and the longitude: 7.3553838

An example, provided by the author of the questionnaire is shown below:

var latlng = lat + "," +lng; 
var url = "maps.googleapis.com/maps/api/geocode/json?latlng=" + latlng + "&sensor=true"; 
$.getJSON(url, function (data) { 
    for(var i=0;i<data.results.length;i++) 
    { 
        var adress = data.results[i].formatted_address; 
        //alert(adress); 
        document.getElementById('endereco_saida_maps').value = adress; 
        endereco_campo.value = adress; 
    } 
});
  • I managed using the url you entered and creating the function: var latlng = lat+"," +lng; var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latlng + "&sensor=true"; $. getJSON(url, Function (data) { for(var i=0;i<data.results.length;i++) { var Adress = data.Results[i]. formatted_address; //Alert(Adress); Document.getElementById('addresse_saida_maps'). value = Adress; address_field.value = Adress; } });`

  • Another way is to use the JS API itself to do this, just check Reverse geocoding

1

Another idea:

function cityByLatLng(latitude, longitude) {
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude);
    geocoder.geocode({'location': latlng}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                document.getElementById('seu elemento').value = results[0].formatted_address;
            } else {
                window.alert('No results found');
            }
        } else {
            window.alert('Geocoder failed due to: ' + status);
        }
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.