Html5 Geolocation to obtain geographic location information, html5geolocation

Source: Internet
Author: User

Html5 Geolocation to obtain geographic location information, html5geolocation

Html5 provides an API for obtaining the current location of a user through a browser. Based on this feature, you can develop location-based service applications. Before obtaining the geographic location information, the browser will first ask the user if they are willing to share their location information, which can be used only after the user's consent.

Html5 uses the getCurrentPosition method provided by the Geolocation API to obtain geographic location information. This method has three parameters: the callback function executed when the geographic location information is obtained successfully, the callback function and optional attribute configuration items that fail to be executed.

The Code is as follows (convertor. js provides the coordinate conversion file for Baidu map ):

<! DOCTYPE html> 

The effects on mobile phones are as follows:

 

BMap. Convertor. translate (gpsPoint, 0, translateCallback); // convert the real longitude and latitude to Baidu coordinates.

GpsPoint var gpsPoint = new BMap. Point (longitude, latitude );

(GPS coordinates) 0: represents GPS; can also be 2: google coordinates; translateCallback: Return Function

 

Convertor. js file:

(Function () {// closure function load_script (xyUrl, callback) {var head = document. getElementsByTagName ('head') [0]; var script = document. createElement ('script'); script. type = 'text/javascript '; script. src = xyUrl; // reference jQuery's script cross-origin method script. onload = script. onreadystatechange = function () {if ((! This. readyState | this. readyState = "loaded" | this. readyState = "complete") {callback & callback (); // Handle memory leak in IE script. onload = script. onreadystatechange = null; if (head & script. parentNode) {head. removeChild (script) ;}}; // Use insertBefore instead of appendChild to circumvent an IE6 bug. head. insertBefore (script, head. firstChild);} function translate (point, type, callback) {var callbackName = 'cbk _ '+ Math. round (Math. random () * 10000); // random function name var xyUrl =" http://api.map.baidu.com/ag/coord/convert?from= "+ Type +" & to = 4 & x = "+ point. lng + "& y =" + point. lat + "& callback = BMap. convertor. "+ callbackName; // dynamically create the script tag load_script (xyUrl); BMap. convertor [callbackName] = function (xyResult) {delete BMap. convertor [callbackName]; // Delete the changed function var point = new BMap after calling. point (xyResult. x, xyResult. y); callback & callback (point) ;}} window. BMap = window. BMap | {}; BMap. convertor ={}; BMap. convertor. translate = translate ;})();

 

What kind of coordinate system does Baidu adopt?

Baidu map api uses two coordinate systems: the longitude and latitude coordinate system and the mocato projection coordinate system. The unit of the former is degree, and the unit of the latter is meter. For specific definitions, refer to the encyclopedia entry description:

Bytes.

Why is Baidu coordinate offset?

The International Latitude and longitude coordinates standard is WGS-84, domestic must at least use the GCJ-02 developed by the national test Bureau, the geographic location is encrypted for the first time. On the basis of Baidu coordinates, the secondary encryption measures of BD-09 are carried out to protect the privacy of individuals. The coordinate system of Baidu's external interface is not the real longitude and latitude of GPS collection, and must be converted through the coordinate conversion interface.

 

W3school demo address: http://www.w3school.com.cn/tiy/t.asp? F = html5_geolocation_error

 

Related Article

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.