Baidu map, AMAP, HTML5 latitude and longitude comparison
Is the longitude and latitude of a location determined? I think many people will answer this question. I'm sure, but what is it actually? I have.
During the use of Baidu map, we found a strange phenomenon. Sometimes, when calling Baidu map js API, we got a wrong place. Then I also studied the jsAPI to obtain the geographical location information. jsAPI actually uses the HTML5 method to obtain geographical information. However, it is strange that the latitude and longitude obtained by HTML5 are quite different from those obtained by Baidu.
Latitude and longitude obtained by HTML5
function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else {alert( Geolocation is not supported by this browser.) }} function showPosition(position) { $(#lngl).val(position.coords.longitude ); $(#latl).val(position.coords.latitude); }
How to obtain the longitude and latitude of a Baidu Map
// Baidu map API function var map = new BMap. map (allmap); var circle = new BMap. geolocation (); var options ={}; options. enableHighAccuracy = true; options. timeout = 10; options. maximumAge = 0; circle. getCurrentPosition (locationResult, options); // enableHighAccuracyBoolean requires the browser to obtain the best result ., TimeoutNumber timeout time ., MaximumAgeNumber allows you to return cache results within a specified period of time. If the value is 0, the browser immediately obtains the new positioning result. Map. addOverlay (circle); var tempGeocoder = new BMap. geocoder (); function locationResult (geolocationResult) {var Status = this. getStatus () if (Status = 0) // the query is successful. Corresponding value "0 ". {$ (# LngBaidu ). val (geolocationResult. point. lng); $ (# latBaidu ). val (geolocationResult. point. lat); var address = geolocationResult. address; $ (# cityBaidu ). val (address. city); $ (# districtBaidu ). val (address. district); $ (# streetBaidu ). val (address. street); else {alert (locate failure error code + Status )}}
How to obtain latitude and longitude from AMAP
// AMap API function var mapObj, geolocation; var MGeocoder; mapObj = new AMap. map ('allmap1'); mapObj. plugin ('amap. geolocation ', function () {geolocation = new AMap. geolocation ({enableHighAccuracy: true, // whether to use high-precision positioning. Default: true timeout: 10000, // stop positioning after 10 seconds. Default: Infinite maximumAge: 0, // The positioning result is cached for 0 milliseconds. The default value is 0 convert: false. // The Automatic Offset coordinates. The coordinates after the offset are AMAP coordinates. The default value is true showButton: false, // The positioning button is displayed. The default value is true buttonPosition: 'lb'. // The Position of the positioning button. The default value is 'lb', and the lower-left value is buttonOffset: new AMap. pixel (10, 20), // offset between the positioning button and the Set dock position. Default Value: Pixel (10, 20) showMarker: false, // After the positioning is successful, the point mark is displayed at the located position. The default value is true showCircle: false. // After the positioning is successful, the positioning precision range is indicated by a circle. The default value is true panToLocation: true, // After the positioning is successful, the located position is used as the map center point. Default Value: true zoomToAccuracy: true // After the positioning is successful, adjust the map field of view to make the positioning position and precision range visible in the field of view, default Value: false}); mapObj. addControl (geolocation); AMap. event. addListener (geolocation, 'complete', onComplete); // return location information AMap. event. addListener (geolocation, 'error', onError); // returns the location error information geolocation. getCurrentPosition () ;}); function onComplete (data) {var lnglatXY = [data. position. getLng (), data. position. getLat ()]; $ (# accuracy ). val (data. accuracy); $ (# lng ). val (data. position. getLng (); $ (# lat ). val (data. position. getLat ();} function onError (data) {var str = 'positioning failed; '; str + = 'error message:' switch (data.info) {case 'permission _ DENIED ': str + = 'browser blocked locating operation'; break; case 'position _ UNAVAILBLE': str + = 'unable to obtain the current location'; break; case 'timeout': str + = 'location timeout'; break; default: str + = 'unknown error'; break;} alert (str );}
The longitude and latitude obtained using these three methods are quite different !!
The first digit after the decimal point may be different. The second digit after the decimal point must be different. The deviation of 0.01 will lead to the deviation of dozens of kilometers. This is still the case after multiple tests.
Finally, I found this in Baidu map's official document.
The Baidu map coordinate conversion API is a set of coordinate conversion interfaces provided in HTTP format, common Non-Baidu coordinates (currently, coordinates of GPS devices, google Maps, soso maps, amap maps, and mapbar maps are supported) the converted coordinates can be used in Baidu map JavaScript API, iov api, static Graph API, and web Service API. Note that the coordinate conversion service for Android SDK, iOS SDK, positioning SDK, and navigation SDK must be applied separately.
Official Baidu map documentation
Only then can we know that the data obtained by HTML5 is the longitude and latitude of Baidu map after being converted. It is no wonder that many tests have been conducted, and almost all of them have such a large deviation.
How to convert HTML5 longitude and latitude to Baidu map longitude and latitude:
Var ggPoint = new BMap. point (position. coords. longpolling, position. coords. latitude); // HTML5 latitude and longitude var convertor = new BMap. convertor (); var pointArr = []; pointArr. push (ggPoint); convertor. translate (pointArr, 1, 5, translateCallback)} // The callback function translateCallback = function (data) {if (data. status = 0) {// data. points [0]; // The converted Baidu coordinates (correct) $ (# lngz ). val (data. points [0]. lng); $ (# latz ). val (data. points [0]. lat );}}
After this conversion, the deviation is basically the same for the first three digits after the decimal point.
Tests show that AMAP directly uses the longitude and latitude returned by HTML5, but does not find the relevant documents. Baidu map uses the transformed longitude and latitude. A simple latitude and longitude problem leads to such a problem that deviates from common sense due to various reasons, which is really hard to understand !!