Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <meta content="yes" name="apple-mobile-web-app-capable" />
  2.  
  3. if (window.navigator.geolocation) {
  4.  
  5. var accuracyThreshold = 100,
  6. timeout = 10 * 1000,
  7. watchID = navigator.geolocation.watchPosition(function(position) {
  8. $('#latitude').val(position.coords.latitude); // set your latitude value here
  9. $('#longitude').val(position.coords.longitude); // set your longitude value here
  10.  
  11. // if the returned distance accuracy is less than your pre-defined accuracy threshold,
  12. // then clear the timeout below and also clear the watchPosition to prevent it from running continuously
  13. position.coords.accuracy < accuracyThreshold && (clearTimeout(delayClear), navigator.geolocation.clearWatch(watchID))
  14. }, function(error) {
  15.  
  16. // if it fails to get the return object (position), clear the timeout
  17. // and cancel the watchPosition() to prevent it from running continuously
  18. clearTimeout(delayClear);
  19.  
  20. navigator.geolocation.clearWatch(watchID);
  21.  
  22. // make the error message more human-readable friendly
  23. var errMsg;
  24.  
  25. switch (error.code) {
  26. case '0':
  27. errMsg = 'Unknown Error';
  28. break;
  29. case '1':
  30. errMsg = 'Location permission denied by user.';
  31. break;
  32. case '2':
  33. errMsg = 'Position is not available';
  34. break;
  35. case '3':
  36. errMsg = 'Request timeout';
  37. break;
  38. }
  39. }, {
  40. enableHighAccuracy: true,
  41. timeout: timeout,
  42. maximumAge: 0
  43. }),
  44. delayClear = setTimeout(function() {
  45. navigator.geolocation.clearWatch(watchID);
  46. }, timeout + 1E3); // make this setTimeout delay one second longer than your watchPosition() timeout
  47. }
  48. else {
  49. throw new Error("Geolocation is not supported.");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement