| Geolocation API | |
|---|---|
| First published | December 22, 2008; 17 years ago (2008-12-22)[1][2] |
| Latest version | W3C Recommendation September 1, 2022; 3 years ago (2022-09-01)[3] |
| Organization | |
| Committee | Geolocation Working Group[3] |
| Editors | Andrei Popescu[3] |
| Domain | Geographical location information |
| Website | www |
TheW3C Geolocation API is an effort by theWorld Wide Web Consortium (W3C) to standardize an interface to retrieve thegeographical location information for a client-side device.[3] It defines a set of objects,ECMAScript standard compliant, that executing in the client application give the client's device location through the consulting ofLocation Information Servers, which are transparent for theapplication programming interface (API). The most common sources of location information areIP address, availableWi-Fi andBluetooth networks,radio-frequency identification (RFID), Wi-Fi connection location, or deviceGlobal Positioning System (GPS) andGSM/CDMA cell IDs. The location is returned with a given accuracy depending on the best location information source available.
Web pages can use the Geolocation API directly if the web browser implements it. Historically, some browsers could gain support via theGoogle Gearsplugin, but this was discontinued in 2010 and the server-side API it depended on stopped responding in 2012.[4][5]
The Geolocation API is ideally suited to web applications for mobile devices such assmartphones. On desktop computers, the W3C Geolocation API works inFirefox since version 3.5,Google Chrome,[6]Opera 10.6,[7]Internet Explorer 9.0,[8] and Safari 5. On mobile devices, it works onAndroid (firmware 2.0+),iOS,Windows Phone andMaemo. The W3C Geolocation API is also supported by Opera Mobile 10.1 – available for Android andSymbian devices (S60 generations 3 & 5) since 24 November 2010.[9] Browsers initially allowed access to the API in insecure contexts, but in the context of Secure Contexts,[10] browsers, e.g., Chrome,[11] now generally require a secure connection.
Google Gears provided geolocation support for older and non-compliant browsers, including Internet Explorer 7.0+ as a Gears plugin, and Google Chrome which implemented Gears natively. It also supported geolocation on mobile devices as a plugin for the Android browser (pre version 2.0) andOpera Mobile forWindows Mobile. However, the Google Gears Geolocation API is incompatible with the W3C Geolocation API and is no longer supported.
Though the implementation is not specified, W3C Geolocation API is built on extant technologies, and is heavily influenced by Google Gears Geolocation API. Example: Firefox's Geolocation implementation[12] uses Google's network location provider.[5] Google Gears Geolocation works by sending a set of parameters that could give a hint as to where the user's physical location is to a network location provider server, which is by default the one provided by Google (code.l.google.com).[13] Some of the parameters are lists of sensed mobile cell towers and Wi-Fi networks, all with sensed signal strengths. These parameters are encapsulated into a JavaScript Object Notation (JSON) message and sent to the network location provider via HTTP POST. Based on these parameters, the network location provider can calculate the location. Common uses for this location information include enforcing access controls, localizing and customizing content, analyzing traffic,contextual advertising and preventing identity theft.[14]
SimpleJavaScript code that checks if the browser has the Geolocation API implemented and then uses it to get the current position of the device. this code creates a function which can be called on HTML using<body onload="geoFindMe()">:
constgeoFindMe=()=>{if(navigator.geolocation){navigator.geolocation.getCurrentPosition(success,error,geoOptions);}else{console.log("Geolocation services are not supported by your web browser.");}}constsuccess=(position)=>{constlatitude=position.coords.latitude;constlongitude=position.coords.longitude;constaltitude=position.coords.altitude;constaccuracy=position.coords.accuracy;console.log(`lat:${latitude} long:${longitude}`);}consterror=(error)=>{console.log(`Unable to retrieve your location due to${error.code}:${error.message}`);}constgeoOptions={enableHighAccuracy:true,maximumAge:30000,timeout:27000};