API maps display address

Asked

Viewed 205 times

-1

Today in my system I am displaying as 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. Look forward to.

I need to display the address in the $html variable, all 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>

2 answers

0

Solved the problem this way, but I identified that the same position (latitude and longitude) inform different addresses in each query, will 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>
<script type="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>";
 ?>

0

  • I get it, I just couldn’t apply it to my script, if you can help me:

  • Give more details. What’s the problem? Have any plunkr or place to see the code?

  • I have updated the initial question in more detail. If you can help me, I would appreciate it very much.

Browser other questions tagged

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