Monday, April 30, 2012

HTML 5 Geolocation Example

Here I will be showing you a simple example of getting your current location using HTML 5 and jQuery.
First we add a the $(document).ready() function to the beginning of our script
Next, we add the code that will instruct the browser to get the current location
The getCurrentPosition(successCallback, failureCallback) function instructs the browser to retrieve the current location. It accepts 2 parameters: the success callback and the error callback. See code below for details.

Once the page loads, you will receive a prompt from your browser asking you to share your current location.


Once you click "Share Location", if the location is found, the success callback will be called and the "position" object will be passed as a parameter. The position.coords object contains the following properties:
latitude, longitude, altitude, accuracy, altitudeAccuracy, heading, and speed. Once the success function is called, it is up to you to do whatever you like with the data.