HTML5 get the user's current location

Source: Internet
Author: User

Geolocation-enabled browsers include ie9+, Firefox 3.5+, Opera 10.6+, Safari 5+, Chrome, IOS Safari, and Android version WebKit.

1Navigator.geolocation.getCurrentPosition (function(position) {//a function that executes successfully2document.write ("Current geographic Latitude:", Position.coords.latitude, "; Longitude:", position.coords.longitude, "; accuracy factor:", Position.coords.accuracy, "rice. <br> "); 3document.write ("Altitude:", position.coords.altitude, "; accuracy factor:", position.coords.altitudeAccuracy, "; compass direction (0° = True North):", Position.coords.heading, "; Moving several degrees:", position.coords.speed);4},function(Error) {//failed Function Code property indicates user denied sharing (1), invalid location (2), or timeout (3)5document.write ("Error type:" + error.code+ "<br>"); 6document.write ("error message:" +error.message); 7 }, { 8Enablehighaccuracy:false,//Enablehighaccuracy is a Boolean value that indicates that the most accurate location information must be used as much as possible (the recommended setting to False,true takes longer and can cause more power consumption on mobile devices)9timeout:5000,//timeout is the maximum time to wait for location information in milliseconds (time-out, if you do not need frequent access to the user's location, the recommended setting is Infinity)Tenmaximumage:25000//Maximumage represents the effective time of the last obtained coordinate information, in milliseconds, if the time is up to regain new coordinate information One});

Note: Tested according to the API call Baidu map shows the current location, test results and native applications (Android, iOS app) get a large location bias, not suitable for navigation and positioning applications.

If you want to track the user's location, you can use another method, Watchposition (). This method receives exactly the same parameters as the GetCurrentPosition () method. In fact, Watchposition () has the same effect as a timed call to GetCurrentPosition (). After the first call to the Watchposition () method, the current position is taken, a successful callback is performed, or an error callback is executed. Then, Watchposition () waits for the system to emit a signal that the position has changed (it does not poll the location itself). Calling Watchposition () returns a numeric identifier that is used to track the monitored operation. Based on this return value, the monitoring operation can be canceled as long as it is passed to the Clearwatch () method (similar to using settimeout () and cleartimeout (). For example:

 1  var  Watchid = Navigator.geolocation.watchPosition (function   (position) { 2   Drawmapcenteredat (Position.coords.latitude, positions.coords.longitude);  3 }, function   4  console.log (" Error code: "+< Span style= "color: #000000;" > Error.code);  5  console.log ("Error message:" + Error.message) ; 6  });  7  

The above example calls the Watchposition () method and saves the returned identifier in Watchid. The Watchid is then passed to Clearwatch () and the monitoring operation is canceled.

HTML5 get the user's current location

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.