HTML and CSS Reference
In-Depth Information
rural areas, the availability of Wi-Fi connectivity is often poor, and location information may become
unavailable.
Mobile Phones (GSM or CDMA)
This technique uses the distance between mobile-phone towers and the user to determine the user's
location. The technique works only with mobile phones and only in the areas where mobile stations are
available. The results are reasonably accurate and can be used for applications specifically developed for
mobile phones.
n
Note
The specific technique used by the Geolocation API is determined by the device and the software that is
using the Geolocation API, and not by the Geolocation API itself.
The Geolocation API
The Geolocation API allow you to perform three operations: find a user's location at a given point in time,
track a user's location as the user moves from one place to another, and stop tracking the user's location.
The API is encapsulated in a
geolocation
object that's available as a property on the browser window's
navigator
object. These three tasks are accomplished with the help of three methods:
getCurrentPosition()
,
watchPosition()
, and
clearWatch()
. These three methods are described in Table
12-1.
Table 12-1.
Methods of the
ge
olocation
Object
Method
Description
getCurrentPosition()
Determines the user's current location. It accepts a
success
function, an
error
function, and an
options
object as parameters. The
success
function is then
used to find the latitude and longitude coordinates of the user's location. You
can use the
error
function to flag the user about an error while determining
the location. The latitude and longitude values are in decimal format.
watchPosition()
Similar to
getCurrentPosition()
but keeps monitoring the user's location
periodically unless the
clearWatch()
method is called. This method is suited
for tracking user movements as the user moves from one place to another.
clearWatch()
Stops monitoring the user's location. This method is triggered using the
watchPosition()
method.
Looking at Table 12-1, you might wonder how these results can benefit the end user. The Geolocation
API merely gives you the user's location. How you use this data in innovative ways is up to you. Consider
the following situations in which the Geolocation API can be of great use:
• Travel companies can use the location to provide a list of nearby pick-up points.
• A mapping application can suggest driving directions to the user based on the
location information.
• A job portal can present only those jobs located within a speciied distance of the
user.