/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>ADC Geolocation Example</title> <!-- An example of the HTML5 Geolocation API for the Adobe Developer Center by Ryan Stewart - [email protected] http://blog.digitalbackcountry.com --> <script type="text/javascript"> // Define the watchID variable so we can turn it off var watchID; // Start getting the location whenever it changes function getLocationConstant() { if(navigator.geolocation) { watchID = navigator.geolocation.watchPosition(onGeoSuccess,onGeoError); } else { alert("Your browser or device doesn't support Geolocation"); } } // Get a single location update function getLocationConstant() { if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError); } else { alert("Your browser or device doesn't support Geolocation"); } } // If we have a successful location update function onGeoSuccess(event) { alert(event.coords.latitude + ', ' + event.coords.longitude); } // If something has gone wrong with the geolocation request function onGeoError(event) { alert("Error code " + event.code + ". " + event.message); } // Called when we want to stop getting location updates function stopGetLocation(event) { navigator.geolocation.clearWatch(watchID); } </script> </head> <body> <input type="button" value="Get Location" onclick="getLocation()" /><br /> <input type="button" value="Toggle Constant Location Update" onclick="getLocationConstant()" /><br /> <input type="button" value="Stop Tracking Location" onclick="stopGetLocation()" /> </body> </html>