All iz Well

Primary Menu

Get Current location using HTML5 GeoLocation API

HTML5 GeoLocation APIThe Geolocation API provides a method to locate the user’s exact (more or less – see below) position. This is useful in a number of ways ranging from providing a user with location specific information to providing route navigation. For privacy reasons, the user is asked for permission to report location information.

Explanation

By using navigator.geolocation.getCurrentPosition function, we can retrive the user location. For privacy reasons, the user is asked for permission to report location information. This method has three parameters( Success, error, options).

Syntax is as follows

navigator.geolocation.getCurrentPosition(success[, error[, options]])

Parameters

success

A callback function that takes a Position object as input parameter. Using this object we can fetch the latitude and longtitude of the current device.

error(Optional)

An optional callback function that takes a PositionError object input parameter. Using this we can find reason of error.

CODE REASON

1 Permission Denied

2 Position Unavailable

3 Time Out

options(Optional)

Containing option properties to pass as a parameter for navigator.geolocation.getCurrentPosition() and navigator.geolocation.watchPosition() functions