Geolocation API JavaScript Access user's current location information

Source: Internet
Author: User

The implementation of the Geolocation API in the browser is a navigator.geolocation object, which is commonly used in the following ways.

1. The first method is getcurrentposition()

Invoking this method triggers a dialog box that asks the user to share geolocation information. For example, a dialog box in Firefox:

This method receives 3 parameters: a success callback function, an optional failure callback function, and an optional option object.

The ① success callback function receives a Position object parameter with two properties:coords and timestamp.

The Coords object contains the following location-related information.

    1. latitude, decimal latitude
    2. longitude, decimal longitude
    3. accuracy, accuracy, M

Some browsers also provide information such as altitude/altitude accuracy/speed. The web is commonly used for latitude and longitude.

For example, display the user's location:

Navigator.geolocation.getCurrentPosition (function(position) {    $ (". Content div.item2"). Text ("Your position in: North latitude" +position.coords.latitude+ "longitude" +Position.coords.longitude);});

The ② failure callback function receives an object that contains two attributes, message and code, which save the error text message, which saves the error code, 1-the user refuses to share, 2-the location is invalid, and 3-times out. In most cases, the error information is saved in the log file. Demonstrate:

    Navigator.geolocation.getCurrentPosition (function(position) {        $ (". Content div.item2"). Text ("Your position in: North latitude" +position.coords.latitude+ "longitude" +position.coords.longitude);    }, function (Error) {        $ (". Content div.item2"). Text (error.code+ "," +error.message);    });

Deny when sharing is denied in Chrome:

will be displayed: Error code 1, information is the user refused to share.

③ The third parameter is the option object, which sets the type of information, there are three options to set:

    1. Enablehighaccuracy, Boolean value, using the most accurate location information possible.
    2. Timeout, maximum time to wait for location information, MS
    3. Maximumage, the last time the coordinate information was obtained, in milliseconds, if the time is up, the coordinate information is obtained again.

Such as:

    Navigator.geolocation.getCurrentPosition (function(position) {        $ (". Content div.item2"). Text ("Your position in: North latitude" +position.coords.latitude+ "longitude" +position.coords.longitude);    }, function (Error) {        $ (". Content div.item2"). Text (error.code+ "," +error.message);    },{        Enablehighaccuracy:false,        timeout:+,        maximumage:     });

It is recommended that the enablehighaccuracy be false, otherwise it will take time to consume power. The Maximumage value can be infinity, which indicates that the last coordinate information is always used.

2. Another method,watchposition(), tracks the user's location. The received parameters are consistent with getcurrentposition ().

The first time the method obtains the position information, it waits for the system to send the signal of position change in place.

You can call the Clearwatch() method to cancel the trace.

The content comes from JavaScript advanced programming.

Geolocation API JavaScript Access user's current location information

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.