HTML and CSS Reference
In-Depth Information
When attempting to retrieve the user's location, the web browser will typically
provide a prompt as to whether using geolocation is allowed.
__________
Retrieving the current position
The
getCurrentPosition()
method is given a parameter for the function to call
when the position has been successfully obtained (which could take several minutes if
GPS is used). Here's an example:
function init() {
// get the current position and call the “loc-
atedSuccess" function when successful
win-
dow.navigator.geolocation.getCurrentPosition(locatedSuccess);
}
function locatedSuccess(geo) {
// log the returned Geoposition object
console.log(geo);
}
window.onload = init;
Note
Depending on the web browser used, this code will likely work only on a live
web server (one running locally is fine). If it is not working, first check that the URL
address of the page includes
http://
at the beginning.
This script will attempt to obtain the current location and then call the
loc-
atedSuccess()
function when it has done so. The location query is done asyn-
chronously so that other processes can continue to function on the page. The function
is handed a parameter that contains a
Geoposition
object that contains information
about the location. The
Geoposition
object contains a
timestamp
property and
coords
property, which contains yet another object, a
Coordinates
object. The
Coordinate
object contains the following properties, which may contain
null
val-
ues depending on the hardware capabilities of your viewing device (for instance, if your
device does not have GPS capabilities, these values will be limited):
•
latitude
: The north-south position on the earth
•
longitude
: The west-east position on the earth
Search WWH ::
Custom Search