Map latitude and longitude conversion (Baidu, Autonavi, Google, Leaflet)

—– Due to the requirements of the project, the longitude and latitude of Baidu map should be converted into Leaflet longitude and latitude. Thanks for the coordinate conversion plug-in (GCOord) —– provided by a brother in Leaflet group

The latitude and longitude of commonly used maps are generally in the following formats: GCJ02, BD09, WGS84(EPSG:4326), EPSG:3857(based on the Mercator coordinate system)

Autonavi, Tencent (using GCJ02)

Google (using GCJ02 and WGS84)

  1. Google’s domestic version of latitude and longitude with GCJ02 (also known as the National Survey office or Mars coordinates)

    The foreign version uses WGS84 (original GPS coordinates)

Baidu (using BD09)

  1. BD09The coordinates, it’s at omegaGCJ02Based on encryption

Leaflet (EPSG:3857 by default, WGS84 can also be used) ——- mainly says how to interconvert xy axis coordinates with latitude and longitude coordinates

  1. EPSG:3857 is special because it is the XY coordinate system, which is different from other coordinates of latitude and longitude, and its value is relatively large, for example: [12914838.35, 4814529.9]

  2. Therefore, it must be converted to latitude and longitude coordinates when used, and the method is as follows:

    // EPSG3857 xy coordinates to latitude and longitude coordinates
    const Convert_EPSG3857_To_Default3857 = (lat, lng) = > {
      let tempLng = lng/20037508.34*180;
      let tempLat = lat/20037508.34*180;
      tempLat = 180/Math.PI*(2*Math.atan(Math.exp(tempLat*Math.PI/180)) -Math.PI/2);
      return {
        lng: lng == 0 ? 0 : tempLng,
        lat: lat == 0 ? 0 : tempLat
      };
    };
    // EPSG3857 coordinates to xy coordinates
    const Convert_Default3857_To_EPSG3857 = (lat, lng) = > {
      let mercator = {};
      let x = lng * 20037508.34 / 180;
      let y = Math.log(Math.tan((90 + lat) * Math.PI / 360))/(Math.PI / 180);
      y = y * 20037508.34 / 180;
      mercator.x = x;
      mercator.y = y;
      return {
        lng: lng == 0 ? 0 : mercator.x,
        lat: lat == 0 ? 0 : mercator.y
      };
    };
    Copy the code

Various latitude and longitude coordinates rotate with each other

  1. The transformation is done through plug-ins

    // Baidu to EPSG3857
    const Convert_BD09_To_EPSG3857 = (lat, lng) = > {
      // Use the tool to convert latitude and longitude to EPSG:3857
      let result = gcoord.transform(
        [lng, lat],        // Latitude and longitude coordinates
        gcoord.BD09,       // Current coordinate system
        gcoord.EPSG3857    // Target coordinate system
      );
      return {
        lng: lng == 0 ? 0 : result[0].lat: lat == 0 ? 0 : result[1]}; };// transfer EPSG3857 to Baidu
    const Convert_EPSG3857_To_BD09 = (lat, lng) = > {
      // Use the tool to convert EPSG:3857 latitude and longitude directly to 100 degrees
      let result = gcoord.transform(
        [lng, lat],    		// Latitude and longitude coordinates
        gcoord.EPSG3857,	// Current coordinate system
        gcoord.BD09         // Target coordinate system
      );
      return {
        lng: lng == 0 ? 0 : result[0].lat: lat == 0 ? 0 : result[1]}; };Copy the code

Coordinate conversion (plug-in GCOord)

Use method of plug-in:

// Introduce the plugin gcoord, (from the plugin documentation)
let result = gcoord.transform(
  [116.403988.39.914266].// Latitude and longitude coordinates
  gcoord.WGS84,               // Current coordinate system
  gcoord.BD09                 // Target coordinate system
);
console.log(result);  / / [116.41661560068297, 39.92196580126834]
// The current and destination coordinates are CRS
Copy the code

Attached with gCOORD document, various latitude and longitude description:

CRS Coordinates format instructions
gcoord.WGS84 [lng,lat] Wgs-84 coordinates, latitude and longitude coordinates obtained by GPS equipment
gcoord.GCJ02 [lng,lat] Gcj-02 coordinate system, latitude and longitude coordinates used by Google Map of China, SOSO map, Aliyun map, MapABC map and Amap
gcoord.BD09 [lng,lat] Bd-09 coordinate system, latitude and longitude coordinates used by Baidu Map
gcoord.BD09LL [lng,lat] With BD09
gcoord.BD09MC [x,y] Bd-09 metric coordinates, metric coordinates adopted by Baidu Map, unit: meter
gcoord.BD09Meter [x,y] With BD09MC
gcoord.Baidu [lng,lat] Baidu coordinate system, bD-09 coordinate system alias, the same as BD-09
gcoord.BMap [lng,lat] Baidu Map, bD-09 coordinate system alias, the same as BD-09
gcoord.AMap [lng,lat] Amap, same as GCJ-02
gcoord.WebMercator [x,y] Web Mercator projection, Mercator projection, same as EPSG3857, unit: m
gcoord.WGS1984 [lng,lat] Wgs-84 coordinate system alias, same as WGS-84
gcoord.EPSG4326 [lng,lat] Wgs-84 coordinate system alias, same as WGS-84
gcoord.EPSG3857 [x,y] WebMercator projection, same as WebMercator, unit: meter
gcoord.EPSG900913 [x,y] WebMercator projection, same as WebMercator, unit: meter

Github-hujiulong /gcoord: Geographic coordinate conversion tool

Reference: Mercator coordinate system and latitude and longitude coordinate system conversion between Mercator coordinate system and latitude and longitude coordinate system conversion – Jianshu.com