Introduction
API Methods
- getCurrentPosition
The getCurrentPosition method is used for a one time query on the location.
- watchPosition
The watchPosition method is used when location updates are also needed. When you want to stop the continued updates for the position, you need to call the clear watch method, using the same watchID value. This is very useful
when you want to track changes such as direction mapping applications.
Both methods are asynchronous. The methods specify a mandatory successCallback argument and can optionally specify an errorCallback function and options for the location query. The various options that can be customized for the location query are:
enableHighAccuracy (precise location but may cause slower response and higher
power usage), timeout (default of zero which indicates no timeout) and
maximumAge (expiration for cached location).
In case there was an error, the error callback is invoked if it was specified in the getCurrentPosition/watchPosition method call. Typical error situations
are if the user has denied permission to access the location information, or the
position is unavailable, timeouts, etc.
If the location was successfully retrieved, the success callback method is
invoked. Once you get the location, you can utilize it in your application.
The result location information is returned in a Position object. The Position object has properties which can include expanded location information. Latitude,
longitude, altitude, accuracy, altitude accuracy, heading, speed, and timestamp.
The Latitude, longitude and accuracy properties are populated whereas the
remaining properties may be populated if supported or may be null.
In this code sample, we will run a very basic script to get the user's location and display the latitude and longitude in a text box. When the "Get my
Location" button is clicked, we check if the geolocation API is supported and if it is supported, a call is made to the getCurrentPosition method of the navigator object. The success callback method (displayResult) displays the latitude and longitude retrieved for the user or in case an error occurred, the error callback gets executed and the error message is displayed to the user.
Sample in Action: Here
are some screenshots of the application running in Mozilla Firefox 6.0.2
Code Sample
Figure: browser prompt for user's permission
Figure: Success - got the location!
Figure: Error
- <!DOCTYPE HTML>
- <html>
- <body>
- <script language="javascript">
- function getUserLoc() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(
- displayResult,
- displayError);
- }
- else {
- setMessage("Geolocation is not supported by this browser");
- }
- }
- function displayResult(position) {
- setMessage("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
- }
- function setMessage(msg) {
- document.forms[0].myLoc.value = msg;
- }
- function displayError(error) {
- var errors = { 1: 'Permission denied', 2: 'Position unavailable', 3: 'Request timeout' };
- setMessage("Error occured: " + errors[error.code]);
- }
- </script>
- <form>
- <input type="button" onClick="getUserLoc( );" value="Get my location"/>
- <input type="label" name="myLoc" size="200"/>
- </form>
- </body>
- </html>

Reynier Lester Claro EscalonaPosted Jun 12, 2018, 5:13 PM
Excellent article, thanks for sharing, in a mobile background does not work, any idea or solution.
Prasanna MuraliPosted May 20, 2016, 11:23 AM
Nice one....
Bhuvanesh MohankumarPosted Apr 30, 2016, 3:11 PM
Good one...
Nithya MohanrajhPosted Mar 25, 2016, 1:15 PM
Nice Article.
Asfend YarPosted Feb 29, 2016, 1:59 PM
good
Santhakumar MunuswamyPosted Jan 5, 2016, 11:13 PM
Thanks for nice sharing
Joe WilsonPosted Jan 5, 2016, 1:09 PM
Thank you for sharing.
Pankaj Kumar ChoudharyPosted Jan 5, 2016, 10:42 AM
Thanks Yashwant Vishwakarma Sir...........
Yashwant VishwakarmaPosted Jan 5, 2016, 7:25 AM
Nice article dude , thanks for sharing !!
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 11:29 PM
Thanks Humayun Kabir Mamun Sir...........
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 11:29 PM
Thanks Arul R Sir.....
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 11:29 PM
Thanks Debendra Dash Sir.........
Humayun Kabir MamunPosted Jan 4, 2016, 10:55 PM
Nice...
Arul RPosted Jan 4, 2016, 10:47 PM
Nice share
Debendra DashPosted Jan 4, 2016, 10:07 PM
nice one pankaj.............
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 8:38 PM
Thanks Sandeep Mittal Sir.......
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 8:37 PM
Thanks Karthikeyan K Sir...........
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 8:37 PM
Thanks P K Yadav Sir......
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 8:37 PM
Thanks Kumaresh Rajalingam Sir.........
Sandeep MittalPosted Jan 4, 2016, 7:26 PM
Nice article...
Karthikeyan KPosted Jan 4, 2016, 1:25 PM
5 Star
Praveen KumarPosted Jan 4, 2016, 12:55 PM
So impressive
Kumaresh RajalingamPosted Jan 4, 2016, 12:51 PM
Cool