{{Short description|Javascript API to geolocate devices}} {{Use dmy dates|date=September 2025}} {{Infobox technology standard | title = Geolocation API | long_name = | image = | caption = | first_published = {{Start date and age|2008|12|22}}<ref>{{Cite web |url=https://www.w3.org/standards/history/geolocation-API |title=Geolocation API Specification 2nd Edition Publication History |publisher=W3C |date=n.d.|access-date=21 April 2021 }}</ref><ref>{{cite web |url=https://www.w3.org/TR/2008/WD-geolocation-API-20081222/ |title=Geolocation API Specification |first=Andrei |last=Popescu |publisher=W3C Geolocation Working Group |date=22 December 2008 |access-date=21 April 2021 }}</ref> | version = W3C Recommendation | version_date = {{Start date and age|2022|09|01}}<ref name="geolocation-spec" /> | preview = | preview_date = | organization = {{Plainlist| * {{abbr|W3C|World Wide Web Consortium}} * Google }} | committee = Geolocation Working Group<ref name="geolocation-spec" /> | editors = Andrei Popescu<ref name="geolocation-spec" /> | authors = | base_standards = | related_standards = | abbreviation = | domain = Geographical location information | license = | website = {{URL|https://www.w3.org/TR/geolocation/}} }}
The '''W3C Geolocation API''' is an effort by the World Wide Web Consortium (W3C) to standardize an interface to retrieve the geographical location information for a client-side device.<ref name="geolocation-spec">{{cite web |url=https://www.w3.org/TR/2008/WD-geolocation-API-20081222/ |title=Geolocation API Specification |first1=Marcos |last1=Cáceres |first2=Reilly |last2=Grant |publisher=W3C Geolocation Working Group |date=1 September 2022 |access-date=8 February 2024 }}</ref> It defines a set of objects, ECMAScript standard compliant, that executing in the client application give the client's device location through the consulting of Location Information Servers, which are transparent for the application programming interface (API). The most common sources of location information are IP address, available Wi-Fi and Bluetooth networks, radio-frequency identification (RFID), Wi-Fi connection location, or device Global Positioning System (GPS) and GSM/CDMA cell IDs. The location is returned with a given accuracy depending on the best location information source available.
== Deployment in web browsers == Web pages can use the Geolocation API directly if the web browser implements it. Historically, some browsers could gain support via the Google Gears plugin, but this was discontinued in 2010 and the server-side API it depended on stopped responding in 2012.<ref>{{cite web |url=https://code.google.com/apis/gears/api_geolocation.html |archive-url=https://web.archive.org/web/20120214212239/https://code.google.com/apis/gears/api_geolocation.html|title=Geolocation API: Gears API: Google Code |website=Google Code |date=9 July 2009 |archive-date=14 February 2012 |access-date=21 April 2021 }}</ref><ref name="code-google-gear-geo-api" />
The Geolocation API is ideally suited to web applications for mobile devices such as smartphones. On desktop computers, the W3C Geolocation API works in Firefox since version 3.5, Google Chrome,<ref>{{cite web |url=https://chrome.blogspot.com/2010/05/new-chrome-stable-release-welcome-mac.html|title=A new Chrome stable release: Welcome, Mac and Linux!|website=Google Chrome Blog |date=25 May 2010 |access-date=21 April 2021 }}</ref> Opera 10.6,<ref>{{cite web |url=http://my.opera.com/desktopteam/blog/2010/07/01/opera-10-60-goes-final |archive-url=https://web.archive.org/web/20111109061925/http://my.opera.com/desktopteam/blog/2010/07/01/opera-10-60-goes-final|title=Opera 10.60 goes final|first=Huib|last=Kleinhout |work=My Opera |publisher=Opera Software |date=1 July 2021 |archive-date=9 November 2011 |access-date=21 April 2021 }}</ref> Internet Explorer 9.0,<ref>{{cite web|title=W3C Geolocation API in IE9 - IEBlog - Site Home - MSDN Blogs|url=http://blogs.msdn.com/b/ie/archive/2011/02/17/w3c-geolocation-api-in-ie9.aspx|url-status=dead|archive-url=https://web.archive.org/web/20110219033657/http://blogs.msdn.com/b/ie/archive/2011/02/17/w3c-geolocation-api-in-ie9.aspx|author=ieblog|website=IEBlog|date=17 February 2011|archive-date=19 February 2011|access-date=21 April 2021 }}</ref> and Safari 5. On mobile devices, it works on Android (firmware 2.0+), iOS, Windows Phone and Maemo. The W3C Geolocation API is also supported by Opera Mobile 10.1 – available for Android and Symbian devices (S60 generations 3 & 5) since 24 November 2010.<ref>{{cite web|url=http://www.opera.com/press/releases/2010/11/24/ |archive-url=https://web.archive.org/web/20111104005142/http://www.opera.com/press/releases/2010/11/24/|author=Opera.com|title=Opera speeds up Symbian browsing|date=25 November 2010|archive-date=4 November 2011|access-date=21 April 2021 }}</ref> Browsers initially allowed access to the API in insecure contexts, but in the context of Secure Contexts,<ref>{{Cite web |title=Secure Contexts |url=https://www.w3.org/TR/secure-contexts/Overview.html |access-date=13 October 2022 |website=W3.org }}</ref> browsers, e.g., Chrome,<ref>{{Cite web |title=Intent to Remove: Insecure origin usage of geolocation |url=https://groups.google.com/a/chromium.org/g/blink-dev/c/ylz0Zoph76A/m/jaMAcld6BQAJ |access-date=13 October 2022 |website=Groups.Google.com }}</ref> 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) and Opera Mobile for Windows Mobile. However, the Google Gears Geolocation API is incompatible with the W3C Geolocation API and is no longer supported.
==Implementation== 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<ref>{{cite web |url=https://support.mozilla.org/en-US/kb/does-firefox-share-my-location-websites?redirectslug=does-firefox-share-my-location-web-sites&redirectlocale=en-US|title=Does Firefox share my location with websites? {{!}} Mozilla Support|work=Mozilla Firefox|author-link=Mozilla Firefox|publisher=Mozilla|date=n.d.|access-date=21 April 2021}}</ref> uses Google's network location provider.<ref name="code-google-gear-geo-api">{{cite web |url=https://code.google.com/apis/gears/geolocation_network_protocol.html|url-status=dead|archive-url=https://web.archive.org/web/20151222162645/https://code.google.com/p/gears/wiki/GeolocationAPI|title=GeolocationAPI - gears - Provides the geolocation of a device running a Gears-enabled web browser. - Improving Your Web Browser - Google Project Hosting|work=Google Code|archive-date=22 December 2015|date=n.d.|access-date=21 April 2021}}</ref> 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).<ref>{{cite web |url=http://webscannotes.com/?page_id=425|url-status=dead|archive-url=https://web.archive.org/web/20121112035756/http://webscannotes.com/geolocation-api/|title=WebScanNotes.com: W3C Geolocation API|work=WebScanNotes|date=n.d.|archive-date=12 November 2012|access-date=21 April 2021}}</ref> 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.<ref>{{cite web|url=https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1622411|ssrn=1622411 |title=Personal Jurisdiction, Internet Commerce, and Privacy: The Pervasive Legal Consequences of Modern Geolocation Technologies|first=Kevin F.|last=King|date=8 June 2010|publication-date=9 June 2010|access-date=21 April 2021}}</ref>
===Example code=== Simple JavaScript 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 <code><body onload="geoFindMe()"></code>: <syntaxhighlight lang="javascript"> const geoFindMe = () => { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error, geoOptions); } else { console.log("Geolocation services are not supported by your web browser."); } }
const success = (position) => { const latitude = position.coords.latitude; const longitude = position.coords.longitude; const altitude = position.coords.altitude; const accuracy = position.coords.accuracy; console.log(`lat: ${latitude} long: ${longitude}`); }
const error = (error) => { console.log(`Unable to retrieve your location due to ${error.code}: ${error.message}`); }
const geoOptions = { enableHighAccuracy: true, maximumAge: 30000, timeout: 27000 }; </syntaxhighlight>
==See also== * Internet geolocation * Local search (Internet) * Location-based service
==References== {{Reflist}}
==External links== * [https://www.w3.org/TR/geolocation/]
{{W3C standards}} {{Web interfaces}}
Category:Application programming interfaces Category:HTML5 Category:Internet geolocation Category:Location-based software Category:Web standards