Geolocation API

The if("geolocation" in navigator) statement checks whether the browser supports Geolocation API or not. The current position of the device is then determined by the getCurrentPosition() method.

html5 geolocation

					if("geolocation" in navigator) {
					    navigator.geolocation.getCurrentPosition(function(position) {
					    	console.log("latitude", position.coords.latitude);
					    	console.log("longitude", position.coords.longitude);    	
					else {
						console.log("Geolocation API is not supported in this browser.");

The success callback function takes a position object as its parameter, which has the coords and timestamp properties that give details about a geographic location. The properties coords.latitude and coords.longitude return the latitude and longitude of the device's position as decimal numbers respectively. Other properties which are returned if available are listed below -

The altitude of the position in metres above the WGS84 ellipsoid.
The accuracy of the latitude and longitude coordinates in metres. If the accuracy value is 2000, then the actual position is within the circle of radius 2000 metres centered at coords.latitude and coords.longitude coordinates.
Similar to coords.accuracy, this property gives the accuracy of the obtained altitude in metres.
The direction of the device in motion, between 0° and 360°, relative to the north in clockwise direction. 0 indicates north, 90 indicates east and 315 indicates north-west. If the value cannot be provided, it is returned as null.
The horizontal speed of the device in metres/sec.


  • Geolocation API is not officially part of HTML 5