主流地圖 座標系轉換,百度、騰訊、高德等
阿新 • • 發佈:2019-01-07
在我國,為了國家安全,電子地圖不可以使用地球座標系WGS84,必須經過偏轉。面前主流的幾款地圖都有其對應的座標系。
例如
高德、騰訊、圖靈、阿里地圖等都是 GCJ-02座標系(也稱火星座標系)
而百度則使用BD-09座標系。
那麼這3個常用座標系直接如何轉換呢。
參照這個連線我總結了一下。http://blog.csdn.net/meegomeego/article/details/39927017
1,GCJ-02(火星)轉BD-09(百度地圖)
邏輯:
void bd_encrypt(double gg_lat, double gg_lon, double &bd_lat, double &bd_lon) { double x = gg_lon, y = gg_lat; double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi); double theta = atan2(y, x) + 0.000003 * cos(x * x_pi); bd_lon = z * cos(theta) + 0.0065; bd_lat = z * sin(theta) + 0.006; }
javaScript語法翻譯:
function MapabcEncryptToBdmap(gg_lat, gg_lon) { var point=new Object(); var x_pi = 3.14159265358979324 * 3000.0 / 180.0; var x = new Number(gg_lon); var y = new Number(gg_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 bd_lon = z * Math.cos(theta) + 0.0065; var bd_lat = z * Math.sin(theta) + 0.006; point.lng=bd_lon; point.lat=bd_lat; //alert("-1:"+point.lng+","+point.lat); return point; }
2,BD-09(百度地圖)轉 GCJ-02(火星)
邏輯:
void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon) { double x = bd_lon - 0.0065, y = bd_lat - 0.006; double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi); double theta = atan2(y, x) - 0.000003 * cos(x * x_pi); gg_lon = z * cos(theta); gg_lat = z * sin(theta); }
javaScript語法翻譯
function BdmapEncryptToMapabc(bd_lat,bd_lon)
{
var point=new Object();
var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
var x = new Number(bd_lon - 0.0065);
var y = new Number(bd_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 Mars_lon = z * Math.cos(theta);
var Mars_lat = z * Math.sin(theta);
point.lng=Mars_lon;
point.lat=Mars_lat;
return point;
}
3,WGS84(地球座標系)轉BD-09(百度座標系)
function GPSToBaidu(lngX,latY){
var gps_point=new BMap.Point(lngX,latY);
BMap.Convertor.translate(gps_point,0,function(bd_point){
alert("lng:"+bd_point.lng+" lat:"+bd_point.lat);
});
}
4,WGS84(地球座標系)轉GCJ-02(火星座標系)
邏輯C#版(幹活了,沒時間了):
using System;
namespace Navi
{
class EvilTransform
{
const double pi = 3.14159265358979324;
//
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
//
// World Geodetic System ==> Mars Geodetic System
public static void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
{
if (outOfChina(wgLat, wgLon))
{
mgLat = wgLat;
mgLon = wgLon;
return;
}
double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
double radLat = wgLat / 180.0 * pi;
double magic = Math.Sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.Sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
mgLat = wgLat + dLat;
mgLon = wgLon + dLon;
}
static bool outOfChina(double lat, double lon)
{
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
static double transformLat(double x, double y)
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
static double transformLon(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
}
}
關於準確性,BD-09和GCJ-02我驗證過了,誤差在10米左右,可以接收。WGS-84和其他座標系直接的轉換待完善和驗證。