Last active
July 24, 2017 16:09
-
-
Save MosheBerman/1f55907b4c0a875b471d8b3652eb4a99 to your computer and use it in GitHub Desktop.
A JS object/function to locate a user with `navigator.geolocation`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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