Geolocation Information API

Source: Internet
Author: User

    1. First, study the Window.navigater object

Window.navigator is used to query some information about the application running the current script. For example,

1 alert ("You ' re using" + navigator.appversion);

The code returns the query to the version number of the current browser. In addition, the Navigator object also has navigator.appname (returning the official name of the browser), Navigator.cookieenabled (returns a Boolean indicating whether the browser has cookies enabled) such as various properties and navigator.vibrate () (if the device supports vibration, trigger device vibration) and other methods. For details, see the first link in the Resources section at the end. The Window.navigater sub-object geolocation is highlighted in the application in this article.

IE9 introduced a built-in location provider, and later HTML5 also provided the API for geolocation information to get the user's current location through a browser. They mainly use Wi-Fi location data and IP protocol address information to locate, detailed principle of the introduction of the computer network (in short, the IP address information contains the corresponding allocation of different regions). The Geolocation API above provides latitude and longitude information to JavaScript by using the Geolocation object in the Web page. One of the most important methods of Geolocation objects is getcurrentposition (), which has the following grammatical form:

Navigator.geolocation.getCurrentPosition (Success, error, options)

The success parameter is a callback function that takes the positional object as its input parameter, the parameter error and options are optional parameters, the error callback function is used to pass in the position error information and feedback, options is some optional location information object. See the second link to the resources for a detailed documentation. Here is an example to illustrate:

1 window.navigator.geolocation.getCurrentPosition (map); 2 3 function Map (position) {4       var latitude = position.coords.latitude; 5       var longitude = position.coords.longitude; 6 };

2. Detect if the browser supports geolocation

When using the Geolocation API, first make sure that the browser supports the Geolocation object, that is, windows.navigator.geolocation is not null.

1 if (window.navigator.geolocation) {2    window.navigator.geolocation.getCurrentPosition ( Success); 3 Else {4       alert ("Your browser does not support HTML5 obtaining geolocation information");5 }

With the support of Geo API in the Web API standard, different browsers may have different problems, this is my question and answer in StackOverflow, if your browser has a problem, please try to see if there is a similar question in these questions and answers.

Http://stackoverflow.com/questions/9215662/navigator-geolocation-getcurrentposition-callbacks-wont-work-on-firefox-10

Http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt

3. Get location information and call Baidu Map API display

1 functionSuccess (position) {2       //get information about latitude and longitude in the location object3       varLongitude =Position.coords.longitude;4       varLatitude =Position.coords.latitude;5 6       //call Baidu Map API, content displayed in HTML in the id "SHOW_MAP" block7       varShow_map =NewBMap ("Show_map");8       varOther_point =Newbmap.point (longitude, latitude);9 Ten       //Conversion Interface OneBMap.Convertor.translate (Other_point, 2,function(point) { A               varMarker =NewBmap.marker (point); - Show_map.addoverlay (marker); -Map.centerandzoom (Point, 15); the       }); -};

Because Google Maps, gold maps and Baidu map for the Latitude standard processing is different, so it is necessary to do the longitude and latitude coordinate conversion, here called Baidu itself has written about the coordinates of the conversion of JavaScript, that is convertor.js. Therefore, you must also remember the references in the file

<type= "Text/javascript"  src= "http://developer.baidu.com/map/ Jsdemo/demo/convertor.js "></script>

about how to find Baidu map of the various interface methods and Convertor.js files, see the link fourth http://developer.baidu.com/map/

4. Related Supplements

Optional parameter An example of the error callback function:

1 function error (ERR) {2       alert ("Error (" + Err.code + "):" + err.message); 3 };

What are the specific errors can be found in Positionerror's documentation Https://developer.mozilla.org/en-US/docs/Web/API/PositionError

Optional parameters options can be set enablehighaccuracy, timeout, Maximumage and other properties.

The specific meaning and setting can refer to Positionoptions's documentation https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions

Resources:

[1]https://developer.mozilla.org/zh-cn/docs/web/api/window.navigator

[2]https://developer.mozilla.org/en-us/docs/web/api/geolocation.getcurrentposition

[3]http://msdn.microsoft.com/zh-cn/library/gg589513 (v=vs.85). aspx

[4]http://developer.baidu.com/map/

Geolocation Information API

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.