HTML and CSS Reference
In-Depth Information
Finding your location
The GeoLocation API is fairly straightforward. let's look at
some code that queries the user's current location.
the
getcurrentPosition
function gets the user's
location.
if (navigator.geolocation) {
check that the
browser supports
the GeoLocation API.
navigator.geolocation.getCurrentPosition(
function (position)
the callback function is
passed a
position
object.
{
pass A function to
getCurrentPosition
document.getElementById('location').innerHTML =
'Latitude: ' + position.coords.latitude +
' Longitude: ' + position.coords.longitude;
that will be called when
the browser has
determined the position.
}
Use the
coords
object to
get the latitude and
longitude.
)
}
The first thing the user will see when they run this code is the browser
asking permission to share their location. Here are examples in Firefox,
Chrome, and the Android browser.