HTML5 Geolocation(Geolocation)
\n\n
HTML5 Geolocation(Geolocation) is used to locate the user's position.
\n\n
Locating the User's Position
\nHTML5 Geolocation API is used to obtain the user's geographical location.
\nGiven that this feature may invade user privacy, unless the user consents, the user's location information is unavailable.
\n\n
Browser Support
\n
Internet Explorer 9+, Firefox, Chrome, Safari and Opera support Geolocation(Geolocation).
\nNote: Geolocation(Geolocation) is more accurate for devices with GPS, such as iPhone.
\n\n
Please use getCurrentPosition() method to obtain user's location.
\nThe following example is a simple geolocation instance that returns the longitude and latitude of the user's location:
\nExample
\nvar x=document.getElementById("demo");\nfunction getLocation(){\nif(navigator.geolocation){\nnavigator.geolocation.getCurrentPosition(showPosition);\n}\nelse{\nx.innerHTML="Geolocation is not supported by this browser.;\n}\n}\nfunction showPosition(position){\nx.innerHTML="Latitude: " + position.coords.latitude +\n"
Longitude: " + position.coords.longitude;\n}\n\n\nInstance analysis:
\n- \n
- Detect whether geolocation is supported \n
- If supported, run the getCurrentPosition() method. If not supported, display a message to the user. \n
- If getCurrentPosition() runs successfully, return a coordinate object to the function specified in the showPosition parameter \n
- The showPosition() function gets and displays longitude and latitude \n
The above example is a very basic geolocation script without error handling.
\n\n
Error Handling and Rejection
\nThe second parameter of the getCurrentPosition() method is used for error handling. It specifies the function to run when getting the user's location fails:
\nExample
\nfunction showError(error){\nswitch(error.code){\ncase error.PERMISSION_DENIED:\nx.innerHTML="User denied the request for Geolocation.\nbreak;\ncase error.POSITION_UNAVAILABLE:\nx.innerHTML="Position unavailable.\nbreak;\ncase error.TIMEOUT:\nx.innerHTML="The request to get user location timed out.\nbreak;\ncase error.UNKNOWN_ERROR:\nx.innerHTML="Unknown error.\nbreak;\n}\n}\n\n\nError codes:
\n- \n
- Permission denied - User does not allow geolocation \n
- Position unavailable - Current location cannot be obtained \n
- Timeout - Operation timed out \n
\n
Displaying Results on Map
\nTo display results on a map, you need to access a map service that can use latitude and longitude, such as Google Maps or Baidu Maps:
\nExample
\nfunction showPosition(position){\nvar latlon=position.coords.latitude+","+position.coords.longitude;\nvar img_url="http://maps.googleapis.com/maps/api/staticmap?center="\n+latlon+"&zoom=14&size=400x300&sensor=false";\ndocument.getElementById("mapholder").innerHTML="
";\n}\n\n\nIn the above example, we use the returned latitude and longitude data to display the location in Google Maps (using a static image).
\n\nThe above link demonstrates how to use scripts to display an interactive map with markers, zoom, and drag options.
\n\n
Information for Given Location
\nThis page demonstrates how to display the user's location on a map. However, geolocation is also useful for information about a given location.
\nCan be used for:
\n- \n
- Updating local information \n
- Showing points of interest around the user \n
- Interactive vehicle navigation system (GPS) \n
\n
getCurrentPosition() Method - Returned Data
\nIf successful, the getCurrentPosition() method returns an object. The latitude, longitude, and accuracy properties are always returned. If available, other properties below will be returned.
\n| Property | \nDescription | \n
|---|---|
| coords.latitude | \nLatitude in decimal degrees | \n
| coords.longitude | \nLongitude in decimal degrees | \n
| coords.accuracy | \nLocation accuracy | \n
| coords.altitude | \nAltitude, in meters above sea level | \n
| coords.altitudeAccuracy | \nAltitude accuracy of the location | \n
| coords.heading | \nDirection, in degrees from true north | \n
| coords.speed | \nSpeed, in meters per second | \n
| timestamp | \nDate/time of the response | \n
\n
Geolocation Object - Other Interesting Methods
\nwatchPosition() - Returns the user's current location and continues to return updated locations as the user moves (like GPS in a car).
\nclearWatch() - Stops the watchPosition() method
\nThe following example shows the watchPosition() method. You need a precise GPS device to test this example (such as an iPhone):
\nExample
\nvar x=document.getElementById("demo");\nfunction getLocation(){\nif(navigator.geolocation){\nnavigator.geolocation.watchPosition(showPosition);\n}\nelse{\nx.innerHTML="Geolocation is not supported by this browser.;\n}\n}\nfunction showPosition(position){\nx.innerHTML="Latitude: " + position.coords.latitude +\n"
Longitude: " + position.coords.longitude;\n}\n\n
YouTip