MENU
Geolocation
The Geolocation API allows you to track the geographical position of the visitor. This feature is available only in secure context (HTTPS).
Geolocation |
Methods: Position getCurrentPosition(function(Position p) success, [,function(PositionError e) error, [, PositionOptions options]]) int watchPosition(function(Position p) success [,function(PositionError e) error [, PositionOptions options]]) void clearWatch(int id) |
watchPosition(...) registers a function that is invoked whenever the location changes. It returns an id that may be passed to clearWarch(...).
RESETRUNFULL
<!DOCTYPE html><html><body style="height:250px">
<script>
var options = {
enableHighAccuracy: true,
timeout: 5000, // 5000ms
maximumAge: 0 // 0ms, ie. don't use cache
};
function success(pos) {
var crd = pos.coords;
document.write('Timestamp: '+ pos.timestamp + '<br/>');
document.write('Latitude : ' + crd.latitude + '<br/>');
document.write('Longitude: ' + crd.longitude + '<br/>');
document.write('Altitude: ' + crd.altitude + '<br/>');
document.write('More or less ' + crd.accuracy + ' meters.' + '<br/>');
document.write('More or less ' + crd.altitudeAccuracy + ' meters for altitude.' + '<br/>');
document.write('Heading: ' + crd.heading + '<br/>');
document.write('Speed: ' + crd.speed);
};
function error(err) {
document.write('ERROR(' + err.code + '): ' + err.message);
};
p = navigator.geolocation.getCurrentPosition(success, error, options);
</script></body></html>
To obtain the name of the place at a particular latitude and longitude, you should use Google Maps API.
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html><html><body>
<p></p>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&key=AIzaSyAgLXVnByvm9VK5t81lJ9ko7i3cMF9KWsA"></script>
<script>
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(5.98,116);
if (geocoder) {
geocoder.geocode({ 'latLng': latLng}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.querySelector("p").innerHTML = results[0].formatted_address;
} else {
document.querySelector("p").innerHTML = "Geocoding failed: " + status;
}
});
}
</script></body></html>