HTML and CSS Reference
In-Depth Information
Figure 7-1. Using the Google Maps API with Geolocation
To access a user's location, run the following JavaScript:
navigator
.
geolocation
.
getCurrentPosition
(
function
(){
//show a map});
The web browser asks the user for permission to reveal his location, as shown in
Figure 7-2
.
Figure 7-2. User alert when accessing Geolocation
After receiving permission, the browser returns a
position
object with a
coords
at‐
tribute. This allows you to call the following properties, from which you can learn the
user's latitude, longitude, and many other data points:
position
.
coords
.
latitude
//geographic coordinate in decimal degrees
position
.
coords
.
longitude
//geographic coordinate in decimal degrees
position
.
coords
.
altitude
//the height of the position (meters above the ellipsoid)