JavaScript Implementation

Contact Us or call 1-877-932-8228
JavaScript Implementation

JavaScript Implementation

For devices that support the GeoLocation API, no external JavaScript file needs to be linked - the JavaScript functionality is inherent in the browser itself. The core of the code is the geolocation object, a child of the navigator object:

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, failureFunction, {enableHighAccuracy:false, maximumAge:30000, timeout:27000});

The if statement tests if the navigator.geolocation object exists on this device/browser. If so, we call the object's getCurrentPosition function - the first two parameters refer to a function (which we've called successFunction, but we could have used any name) to call when getCurrentPosition is successful in finding the user's location, and a function (failureFunction, in this example) to call when getCurrentPosition was not successful in finding the user's location; this parameter is optional. Both failureFunction and successFunction are functions we will write to handle the success and failure cases. The third parameter, a JSON-formatted hash of options, allows fine control over implementation details; we will omit this optional parameter when calling getCurrentPosition.