Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <meta content="yes" name="apple-mobile-web-app-capable" />
- if (window.navigator.geolocation) {
- var accuracyThreshold = 100,
- timeout = 10 * 1000,
- watchID = navigator.geolocation.watchPosition(function(position) {
- $('#latitude').val(position.coords.latitude); // set your latitude value here
- $('#longitude').val(position.coords.longitude); // set your longitude value here
- // if the returned distance accuracy is less than your pre-defined accuracy threshold,
- // then clear the timeout below and also clear the watchPosition to prevent it from running continuously
- position.coords.accuracy < accuracyThreshold && (clearTimeout(delayClear), navigator.geolocation.clearWatch(watchID))
- }, function(error) {
- // if it fails to get the return object (position), clear the timeout
- // and cancel the watchPosition() to prevent it from running continuously
- clearTimeout(delayClear);
- navigator.geolocation.clearWatch(watchID);
- // make the error message more human-readable friendly
- var errMsg;
- switch (error.code) {
- case '0':
- errMsg = 'Unknown Error';
- break;
- case '1':
- errMsg = 'Location permission denied by user.';
- break;
- case '2':
- errMsg = 'Position is not available';
- break;
- case '3':
- errMsg = 'Request timeout';
- break;
- }
- }, {
- enableHighAccuracy: true,
- timeout: timeout,
- maximumAge: 0
- }),
- delayClear = setTimeout(function() {
- navigator.geolocation.clearWatch(watchID);
- }, timeout + 1E3); // make this setTimeout delay one second longer than your watchPosition() timeout
- }
- else {
- throw new Error("Geolocation is not supported.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement