HTML5 Get geolocation information

Source: Internet
Author: User
Tags list of attributes

basic knowledge of Geolocation APIIn HTML5, a new geolocation property has been added to the Window.navigator object, which can be accessed using the Geolocation API. There are three methods for the Geolocation property of a Window.navigator object. Get current location You can use the GetCurrentPosition () method to obtain the user's current geo-location information, which is defined as follows: JS code collection code void GetCurrentPosition (onsuccess, OnError,  Options); The first parameter is the callback function that was executed when the current geo-location information was obtained, the second parameter is the callback function that was executed when the current geo-location information failed, and the third parameter is a list of some optional attributes. Where the latter two parameters are optional.  The callback function that is executed when the method successfully obtains geolocation information is as follows: JS Code Collection Code navigator.geolocation.getCurrentPosition (function (position) {//Get successful processing.  }); If obtaining geo-location information fails, the error message can be prompted to the user through a callback function defined by the second parameter of the method. When you open a page in the browser that uses the Geolocation API to get the user's current location information, the browser asks the user if they want to share the location information and can cause an error if they refuse to share. The callback function uses an Error object as a parameter with two properties: The Code property, the Message property. The possible values for the Code property are: The user rejects the location service (attribute value "1"), gets the location information (property value "2"), Gets the information timeout (property value "3"). The message property is a string that contains an error message that is useful for development and debugging purposes. Some browsers do not support this property. The error handling callback function is used as follows: JS Code Collection Code navigator.geolocation.getCurrentPosition (function (position) {}, function (Error) {var error      Type = [' Location service is denied ', ' get no location information ', ' Get information timeout '];  Alert (errortype[error.code-1]);  }); The third parameter of the method is an optional list of attributes, which include: enablehighaccuracy: Whether high-precision geographic locations are requiredInformation, this parameter is set on many devices is useless, because the use of the device needs to be combined with the amount of power, the specific geographical situation to consider synthetically. Therefore, in most cases this property is the default and is adjusted by the device itself. Timeout: A timeout limit (in milliseconds) for obtaining geo-location information. If geolocation information is not obtained within that time, an error is returned. Maximumage: The effective time, in milliseconds, to cache geo-location information. The cached geo-location information is discarded after this time and attempts to regain geo-location information. If the value is set to "0", the new geo-location information is re-acquired unconditionally.  This property is used as follows: JS Code Collection Code navigator.geolocation.getCurrentPosition (function (position) {}, function (Error) {},///Below is an optional attribute.      {MAXIMUMAGE:60 * 1000 * 2,//Set cache validity time is 2 minutes.  timeout:5000}); Continuous monitoring of current geo-location information Use the Watchposition () method to continuously obtain the user's current geo-location information, which is periodically automatically obtained, as defined by the following: JS code collection code int watchcurrentposition (onsuccess,  OnError, Options); The three parameters of this method are described in the same way as the GetCurrentPosition () method. The method returns a number that is used in a way similar to the return result of the SetInterval () method in a JavaScript script and can be used by the Clearwatch () method to stop monitoring the current geo-location information. Stop getting geo-location information for the current user use this method to stop monitoring the current user's geo-location information.  The method is defined as follows: JS code collection code void Clearwatch (Watchid); The parameter of the method is the return parameter when the Watchcurrentposition () method is called to monitor geolocation information. Position object If obtaining geolocation information succeeds, you can obtain these geolocation information by accessing the properties of the position object in a successful callback function. The Position object has the following properties: Latitude: The latitude of the current geographic location. Longitude: The longitude of the current geographic location. Altitude: The altitude of the current geographic location (NULL if not available). Accuracy: Gets the latitude or longitudeThe precision of the (in meters). Altitudeaccurancy: The accuracy (in meters) of the altitude obtained. Heading: The direction in which the device is moving forward. Represented by a clockwise rotation angle to the north of the face (Null is not available). Speed: The rate at which the device advances (in meters per second, null if not available). Timestamp: Time to obtain geo-location information.

Citation Source: http://lucifinilhades.iteye.com/blog/1226959

HTML5 Get geolocation 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.