var loc = navigator.geolocation
The above function can also be written without creating a navigator object as shown below:
Displaying Location using HTML Geolocation: The following code is displaying the current location with the help of latitude and longitude via HTML Geolocation.
Error and Rejection Handling: It is very important to handle the errors generated in Geolocation and show a required message when an error occurs. Functions like getCurrentPosition() make use of an error handler to handle the error generated (function errHand as used in above example). The PositionError object used by error handler has two properties which let the function handle error efficiently:
Generated errors in geolocation:
|UNKNOWN_ERRROR||An unknown error|
|PERMISSION_DENIED||The application doesn’t have the permission to make use of location services|
|POSITION_UNAVAILABLE||The location of the device is uncertain|
|TIMEOUT||Time to fetch location information exceeded the maximum timeout interval|
Displaying result in MAP: Displaying location in a map is a very interesting task. This services used to provide the exact location in map.
Location Properties: The following table determines properties used in getCurrentPosition() and their returning values.
|coords.latitude||Always returns latitude as a decimal number|
|coords.accuracy||Always returns accuracy of position|
|coords.longitude||Always returns longitude as a decimal number|
|coords.altitude||Returns the altitude in meters above sea level if available|
|coords.altitudeAccuracy||Returns altitude accuracy of position if available|
|coords.heading||Returns heading in degree clockwise from North if available|
|coords.speed||Returns speed in mps if available|
|timestamp||Returns date or time of response if available|
Geolocation Methods: The Geolocation has following methods which make it interesting and easier to work.
|getCurrentPosition()||fetches the current location of the user|
|watchPosition()||fetches periodic updates of user’s current location|
|clearWatch()||cancels a watchPosition call currently in execution|
- HTML | DOM Geolocation coordinates Property
- HTML | DOM Geolocation position Property
- HTML | Navigator geolocation Property
- HTML Course | Structure of an HTML Document
- HTML Course | Basics of HTML
- HTML | DOM HTML Object
- HTML | <map> Tag
- HTML | th Tag
- HTML | br Tag
- Div Tag | HTML
- HTML | <hr> Tag
- HTML | col Tag
- PHP vs HTML
- HTML | dl Tag
- HTML | <q> Tag
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.