Skip to content

Instantly share code, notes, and snippets.

@MosheBerman
Last active July 24, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MosheBerman/1f55907b4c0a875b471d8b3652eb4a99 to your computer and use it in GitHub Desktop.
Save MosheBerman/1f55907b4c0a875b471d8b3652eb4a99 to your computer and use it in GitHub Desktop.
A JS object/function to locate a user with `navigator.geolocation`.
function Locator(successCallback, errorCallback)
{
this.success = (location) =>
{
console.dir(location);
this.pauseLocationUpdates();
successCallback(location);
};
this.error = () =>
{
console.log("Failed to access location");
errorCallback();
};
this.pauseLocationUpdates = () =>
{
if(this.watchID !== null)
{
navigator.geolocation.clearWatch(this.watchID);
this.watchID = null;
}
};
this.options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
this.locate = () =>
{
var browserSupportsLocation = "geolocation" in navigator;
if (browserSupportsLocation)
{
this.watchID = navigator.geolocation.watchPosition(this.success, this.error, this.options);
}
else
{
this.error();
}
};
}
function Locator(successCallback, errorCallback)
{
this.success = function(location)
{
console.dir(location);
this.pauseLocationUpdates();
successCallback(location);
};
this.error = function()
{
console.log("Failed to access location");
errorCallback();
};
this.pauseLocationUpdates = function()
{
if(this.watchID !== undefined)
{
navigator.geolocation.clearWatch(this.watchID);
this.watchID = null;
}
};
this.options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
this.locate = function()
{
var browserSupportsLocation = "geolocation" in navigator;
if (browserSupportsLocation)
{
this.watchID = navigator.geolocation.watchPosition(this.success, this.error, this.options);
}
else
{
this.error();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment