在我国,为了国家安全,电子地图不可以使用地球坐标系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;
}
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;
}
逻辑:
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);
}
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);
});
}
逻辑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和其他坐标系直接的转换待完善和验证。