The use of js map library and the mutual transformation between several coordinates

Posted by MBDesktop on Tue, 04 Jan 2022 02:19:10 +0100

@

1: The use of js map library and the mutual transformation between several coordinates

Introduction: now most of the maps on the market are: Tencent, Gaode, Tencent and these three categories. Gaode Baidu usually uses a lot. Tencent also uses location because of small programs or chatting. So it's used a lot.
But: there will be many different expressions of longitude and latitude. Here's a summary.

WGS-84 coordinate system: the original coordinate system of GPS

In China, all products are not allowed to directly use this coordinate system for direct positioning, because many GPS devices directly obtain GPS signals to obtain longitude and latitude of 84. If you want to make a map based on this, it will be leaked, right.

GCJ-02 coordinate system: national survey of China, Mars coordinate system

The coordinate system released by China National Survey Bureau in 2002 is an encryption algorithm for longitude and latitude data, that is, adding random deviation. All Internet maps of China must be encrypted at least for the first time using GCJ-02, and no coordinate system can be converted to WGS-84 coordinates.

BD-09 coordinate system: Baidu map is obtained by using GCJ-02 further migration algorithm

2: Coordinate system used by each map:

3: Mutual conversion: (BD09--GCJ02)

It is also used that these official coordinate transformations have official APIs. You like wgs-84 to bd-09. Baidu map has official website APIs

Portal:

Baidu map coordinate picking http://api.map.baidu.com/lbsapi/getpoint/index.html
Gaode map coordinate picking https://lbs.amap.com/tools/picker
Tencent map coordinate picking https://lbs.qq.com/getPoint/

Transfer to Tencent map official API https://lbs.qq.com/service/webService/webServiceGuide/webServiceTranslate

Baidu coordinates official API https://lbsyun.baidu.com/jsdemo.htm#TranslateoriTobd

/**
* Coordinate conversion: Baidu map coordinates are converted into Tencent map coordinates
* lng Tencent longitude (pointy)
* lat Tencent latitude (pointx)
* Longitude > latitude
*/
function bMapToQQMap(lng, lat) {

    if (lng == null || lng == '' || lat == null || lat == '')
        return [lng, lat];

    var x_pi = 3.14159265358979324;
    var x = parseFloat(lng) - 0.0065;
    var y = parseFloat(lat) - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    var lng = (z * Math.cos(theta)).toFixed(7);
    var lat = (z * Math.sin(theta)).toFixed(7);

    return [lng, lat];

}

/**
* Coordinate conversion, Tencent map into Baidu map coordinates
* lng Tencent longitude (pointy)
* lat Tencent latitude (pointx)
* Longitude > latitude
*/

function qqMapToBMap(lng, lat) {

    if (lng == null || lng == '' || lat == null || lat == '')
        return [lng, lat];

    var x_pi = 3.14159265358979324;
    var x = parseFloat(lng);
    var y = parseFloat(lat);
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    var lng = (z * Math.cos(theta) + 0.0065).toFixed(5);
    var lat = (z * Math.sin(theta) + 0.006).toFixed(5);
    return [lng, lat];

}

4: Calculate the distance between longitude and latitude:

js:

function toRad(d) {  return d * Math.PI / 180; }
	function getDisance(lat1, lng1, lat2, lng2) {
	    var dis = 0;
	    var radLat1 = toRad(lat1);
	    var radLat2 = toRad(lat2);
	    var deltaLat = radLat1 - radLat2;
	    var deltaLng = toRad(lng1) - toRad(lng2);
	    var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
	    return dis * 6378137;
	} 
	console.log(  getDisance(39.91917,116.3896,39.91726,116.3940) );

mysql:

#The table structure is as follows
CREATE TABLE `map` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `lat` decimal(12,6) NOT NULL,
  `lng` decimal(12,6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
SELECT id,lat,lng,((
2*ATAN2(SQRT(SIN((39.91578-lat)*PI()/180/2)   
        *SIN((39.91578-lat)*PI()/180/2)+   
        COS(lat*PI()/180)*COS(39.91578*PI()/180)   
        *SIN((116.3899-lng)*PI()/180/2)   
        *SIN((116.3899-lng)*PI()/180/2)),   
        SQRT(1-SIN((39.91578-lat)*PI()/180/2)   
        *SIN((39.91578-lat)*PI()/180/2)   
        +COS(lat*PI()/180)*COS(39.91578*PI()/180)   
        *SIN((116.3899-lng)*PI()/180/2)   
        *SIN((116.3899-lng)*PI()/180/2))))*6378140) as len FROM `map` order by len asc ;

Topics: Javascript