GeoCode

by giacal

HTML

<p>
<strong>Rilevazione delle <span data-time></span>:</strong><br/>
    Ti trovi in zona <span data-zona></span> con una approssimazione di <span data-approx></span> metri.
</p>

<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCdneRMEuZApMJr4bEuqHPmZIO5u_gJTQU&sensor=true"></script>

JavaScript

navigator.geolocation.getCurrentPosition(
    fillText,
    function(e){alert("Error code : "+e.code);},
    {maximumAge: 1000}
);

function fillText(position){
    var e_time = document.querySelector('span[data-time]'),
        e_zona = document.querySelector('span[data-zona]'),
        e_appr = document.querySelector('span[data-approx]'),
        time_s = new Date(position.timestamp),
        lat = position.coords.latitude,
        lng = position.coords.longitude,
        geocoder = new google.maps.Geocoder();
    
    e_time.textContent = "" + time_s.getHours() + " e " + time_s.getMinutes();
    e_appr.textContent = position.coords.accuracy;

    geocoder.geocode(
        {'latLng': new google.maps.LatLng(lat, lng) }, 
        function (results, status) {
            e_zona.textContent = results[0].formatted_address;
        }
    );    
}