API maps display address

-1

Today in my system I am displaying as a reference point data already registered in the system based on latitude and longitude. I would like the script to find the reference provided by the maps API itself, only me providing the current latitude and longitude. I await.

I need to display the address in the $ html variable, the rest of the code is ok:

<?php
$html .= "<td>".exibir aqui endereço."</td>";
 ?>


<script language = "javascript">
function GetAddress() {
var lat = parseFloat('$lat');
var lng = parseFloat('$lon');
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = geocoder = new google.maps.Geocoder();
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
  alert("Location: " + results[1].formatted_address);
}
}
});
} 
</script>
    
asked by anonymous 07.01.2018 / 02:12

2 answers

0

You can use Google Maps Geocoding API (see Reverse Geocoding)

or link there is a DEMO of what you want, it's worth taking a look.

    
07.01.2018 / 16:29
0

Resolved the problem in this way, but I have identified that the same position (latitude and longitude) inform different addresses in each query, will it be an API bug or something wrong in the script?

<script type="text/javascript" 
src="http://maps.googleapis.com/maps/api/js?sensor=false"></script><scripttype="text/javascript">
    var lat = parseFloat(<?php echo $RS["vl_latitude"]; ?>);
    var lng = parseFloat(<?php echo $RS["vl_longitude"]; ?>);
    var latlng = new google.maps.LatLng(lat, lng);
    var geocoder = geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                meuEndereco = 'Location: ' + results[1].formatted_address;
            }
        }
    });
</script>     
<?php
  $enderecosEmJavaScript = "<script>document.write(meuEndereco)</script>";
  $html .= "<td>".$enderecosEmJavaScript."</td>";
 ?>
    
09.01.2018 / 17:44