ADC HTML5 Geolocation API Example


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>ADC Geolocation Example</title>
  6.  
  7. <!--
  8. An example of the HTML5 Geolocation API for the
  9. Adobe Developer Center by Ryan Stewart - [email protected]
  10. http://blog.digitalbackcountry.com
  11. -->
  12.  
  13. <script type="text/javascript">
  14. // Define the watchID variable so we can turn it off
  15. var watchID;
  16.  
  17. // Start getting the location whenever it changes
  18. function getLocationConstant()
  19. {
  20. if(navigator.geolocation)
  21. {
  22. watchID = navigator.geolocation.watchPosition(onGeoSuccess,onGeoError);
  23. } else {
  24. alert("Your browser or device doesn't support Geolocation");
  25. }
  26. }
  27.  
  28. // Get a single location update
  29. function getLocationConstant()
  30. {
  31. if(navigator.geolocation)
  32. {
  33. navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
  34. } else {
  35. alert("Your browser or device doesn't support Geolocation");
  36. }
  37. }
  38.  
  39. // If we have a successful location update
  40. function onGeoSuccess(event)
  41. {
  42. alert(event.coords.latitude + ', ' + event.coords.longitude);
  43. }
  44.  
  45. // If something has gone wrong with the geolocation request
  46. function onGeoError(event)
  47. {
  48. alert("Error code " + event.code + ". " + event.message);
  49. }
  50.  
  51. // Called when we want to stop getting location updates
  52. function stopGetLocation(event)
  53. {
  54. navigator.geolocation.clearWatch(watchID);
  55. }
  56. </script>
  57. </head>
  58.  
  59. <body>
  60. <input type="button" value="Get Location" onclick="getLocation()" /><br />
  61. <input type="button" value="Toggle Constant Location Update" onclick="getLocationConstant()" /><br />
  62. <input type="button" value="Stop Tracking Location" onclick="stopGetLocation()" />
  63. </body>
  64. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.