HTML5 Geolocation (Geolocation)
HTML5 Geolocation (Geolocation) is used to locate a user's position.
Locating the User's Position
The HTML5 Geolocation API is used to obtain a user's geographic location.
Since this feature may infringe on user privacy, the user's location information is unavailable unless the user agrees.
Browser support





Internet Explorer 9+, Firefox, Chrome, Safari, and Opera support Geolocation (geolocation).
**Note: **Geolocation (geolocation) is more precise on devices with GPS, such as the iPhone.
HTML5 - Using Geolocation
Use the getCurrentPosition() method to obtain the user's position.
The following example is a simple geolocation instance that returns the longitude and latitude of the user's position:
Example
var
x
=
document
.
getElementById
(
"
demo
"
)
;
function
getLocation
(
)
{
if
(
navigator
.
geolocation
)
{
navigator
.
geolocation
.
getCurrentPosition
(
showPosition
)
;
}
else
{
x
.
innerHTML
=
"
This browser does not support obtaining geographic location.
"
;
}
}
function
showPosition
(
position
)
{
x
.
innerHTML
=
"
Latitude:
"
position
.
coords
.
latitude
"
Longitude:
"
position
.
coords
.
longitude
;
}
Try it »
Example explanation:
- Detect Whether Geolocation Is Supported
- If supported, run the getCurrentPosition() method. If not supported, display a message to the user.
- If getCurrentPosition() runs successfully, it returns a coordinates object to the function specified in the showPosition parameter
- The showPosition() function obtains and displays the longitude and latitude
The example above is a very basic geolocation script without error handling.
Handling Errors and Refusals
The second parameter of the getCurrentPosition() method is used to handle errors. It specifies the function to run when obtaining the user's position fails:
Example
function
showError
(
error
)
{
switch
(
error
.
code
)
{
case
error
.
PERMISSION_DENIED
:
x
.
innerHTML
=
"
The user denied the request for geographic location.
"
break
;
case
error
.
POSITION_UNAVAILABLE
:
x
.
innerHTML
=
"
The position information is unavailable.
"
break
;
case
error
.
TIMEOUT
:
x
.
innerHTML
=
"
The request for the user's geographic location timed out.
"
break
;
case
error
.
UNKNOWN_ERROR
:
x
.
innerHTML
=
"
An unknown error occurred.
"
break
;
}
}
Try it »
Error codes:
- Permission denied - the user did not allow geolocation
- Position unavailable - the current position cannot be obtained
- Timeout - the operation timed out
Displaying the Result on a Map
To display the result on a map, you need to access a map service that can use longitude and latitude, such as Google Maps or Baidu Maps:
Example
function
showPosition
(
position
)
{
var
latlon
=
position
.
coords
.
latitude
"
,
"
position
.
coords
.
longitude
;
var
img_url
=
"
http://maps.googleapis.com/maps/api/staticmap?center=
"
latlon
"
&zoom=14&size=400x300&sensor=false
"
;
document
.
getElementById
(
"
mapholder
"
)
.
innerHTML
=
"
"
;
}
Try it »
In the example above, we use the returned longitude and latitude data to display the position on Google Maps (using a static image).
Google Maps script
The link above demonstrates how to use a script to display an interactive map with markers, zoom, and drag options.
Information About a Given Position
This page demonstrates how to display the user's position on a map. However, geolocation is also very useful for information about a given position.
Can be used to:
- Update local information
- Display points of interest around the user
- Interactive in-car navigation systems (GPS)
getCurrentPosition() Method - Returned Data
T If successful, the getCurrentPosition() method returns an object. The latitude, longitude, and accuracy properties are always returned. Other properties below are returned if available.
| property | Description |
|---|---|
| coords.latitude | Latitude in decimal degrees |
| coords.longitude | Longitude in decimal degrees |
| coords.accuracy | Accuracy of the position |
| coords.altitude | Altitude, in meters above sea level |
| coords.altitudeAccuracy | Accuracy of the altitude |
| coords.heading | Heading, in degrees from true north |
| coords.speed | Speed, in meters per second |
| timestamp | Date/time of the response |
Geolocation Object - Other Interesting Methods
watchPosition() - returns the user's current position and continues to return updated positions as the user moves (like GPS in a car).
clearWatch() - stops the watchPosition() method
The following example demonstrates the watchPosition() method. You need a precise GPS device to test this example (such as an iPhone):
Example
var
x
=
document
.
getElementById
(
"
demo
"
)
;
function
getLocation
(
)
{
if
(
navigator
.
geolocation
)
{
navigator
.
geolocation
.
watchPosition
(
showPosition
)
;
}
else
{
x
.
innerHTML
=
"
This browser does not support obtaining geographic location.
"
;
}
}
function
showPosition
(
position
)
{
x
.
innerHTML
=
"
Latitude:
"
position
.
coords
.
latitude
"
Longitude:
"
position
.
coords
.
longitude
;
}
Try it »