W3C Geolocation API

from Wikipedia, the free encyclopedia

The W3C Geolocation API is a unified Web browser - programming interface to determine the geographic location of the associated terminal. The API defines a number of ECMAScript objects that can be used to read out information about the geographic position of the device once or permanently. The position itself is determined by the browser or operating system on the respective end device and is not part of the API. The sources of the location information are geo-targeting of IP addresses , WLAN-based location , the cell information of the cellular network ( GSM / CDMA2000 ) or, especially in smartphones , the global positioning system . Since these sources have extreme differences in their accuracy (from a few meters to several kilometers), the API returns a value for their accuracy in addition to the most accurate currently available position data.

Browser support

While Geolocation API is now supported by practically all current versions of the popular mobile and desktop browsers, when it was first introduced this technology was often only available via the browser plug-in Google Gears, which has now been discontinued .

use

The position data is determined asynchronously. The data is therefore not available in real time, but is returned by the API to a callback function as soon as it has been determined. For security and data protection reasons, the query for the position data usually has to be confirmed by the user of the website.

The following JavaScript code checks whether the geolocation API is available in the browser showPositionand, if successful, transfers it to the function , which then Alertoutputs the position and, if available, the height information of the device as a message. A second function is showErrorused to intercept and output the errors that may occur when determining the geographic coordinates. The third parameter can be used to set additional options.

if (navigator.geolocation) {
    var options = {
      enableHighAccuracy: true,
    }
    navigator.geolocation.getCurrentPosition(showPosition, showError, options);
} else {
    alert('Ihr Browser unterstützt die W3C Geolocation API nicht.');
}

function showPosition(position) {
    alert(
        'Die geographische Position dieses Geräts ist (Stand: ' + new Date(position.timestamp).toLocaleTimeString() + '):\n'+
        'Breitengrad: ' + position.coords.latitude + '° \n'+
        'Längengrad: ' + position.coords.longitude + '° \n'+
        '  Genauigkeit: ' + position.coords.accuracy + 'm\n' +
        (position.coords.altitude ? ('Höhe: ' + position.coords.altitude + 'm\n' +
                                    '  Genauigkeit: ' + position.coords.altitudeAccuracy + 'm') : "")
    ) ;
}

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            alert('Sie haben die Abfrage ihrer Position untersagt.');
            break;
        case error.POSITION_UNAVAILABLE:
            alert('Es sind keine Positionsdaten verfügbar.');
            break;
        case error.TIMEOUT:
            alert('Das Timeout für die Ortsanfrage wurde überschritten.');
            break;
        default:
            alert('Es ist ein unbekannter Fehler aufgetreten (#' + error.code + ': ' + error.message + ')');
            break;
    }
}

Web links

Individual evidence

  1. W3C Geolocation API Specification . Editor: Andrei Popescu, Google Inc., December 22, 2008. Retrieved July 7, 2009
  2. Can I use geolocation? CanIUse.com, accessed on June 7, 2014 (database for browser support for various web technologies).