在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系:
WGS-84原始坐标系,一般用国际GPS纪录仪记录下来的经纬度,通过GPS定位拿到的原始经纬度,Google和高德地图定位的的经纬度(国外)都是基于WGS-84坐标系的。但是在国内是不允许直接用WGS84坐标系标注的,必须经过加密后才能使用。
GCJ-02坐标系,又名“火星坐标系”,是我国国测局独创的坐标体系,由WGS-84加密而成,在国内,必须至少使用GCJ-02坐标系,或者使用在GCJ-02加密后再进行加密的坐标系,如百度坐标系。高德和Google在国内都是使用GCJ-02坐标系,可以说,GCJ-02是国内最广泛使用的坐标系。
百度坐标系:bd-09,百度坐标系是在GCJ-02坐标系的基础上再次加密偏移后形成的坐标系,只适用于百度地图。(目前百度API提供了从其它坐标系转换为百度坐标系的API,但却没有从百度坐标系转为其他坐标系的API)。
以下为WGS-84、GCJ02、BD-09的坐标系之间相互转换的方法:
///
/// 圆周率
///
private const double PI = 3.14159265358979324;
private const double X_PI = 3.14159265358979324 * 3000.0 / 180.0;
private const double A = 6378245.0;
private const double EE = 0.00669342162296594323;
private const double LON_BOUNDARY_MIN = 72.004;
private const double LAT_BOUNDARY_MIN = 0.8293;
private const double LON_BOUNDARY_MAX = 137.8347;
private const double LAT_BOUNDARY_MAX = 55.8271;
///
/// 是否中国境内坐标
///
///
///
///
private static bool OutOfChina(double gpsLat, double gpsLng)
{
if (gpsLng < LON_BOUNDARY_MIN || gpsLng > LON_BOUNDARY_MAX)
{
return true;
}
if (gpsLat < LAT_BOUNDARY_MIN || gpsLat > LAT_BOUNDARY_MAX)
{
return true;
}
return false;
}
#region WGS坐标系与GCJ02坐标系互转
///
/// WGS84坐标系转GCJ02坐标系
///
/// WGS坐标,纬度
/// WGS坐标,经度
/// GCJ02坐标,纬度
/// GCJ02坐标,经度
public static void WGS84_to_GCJ02(double wgsLat, double wgsLng, out double gcjLat, out double gcjLng)
{
if (OutOfChina(wgsLat, wgsLng))
{
gcjLat = wgsLat;
gcjLng = wgsLng;
}
else
{
double dLat = TransformLat(wgsLng - 105.0, wgsLat - 35.0);
double dLon = TransformLon(wgsLng - 105.0, wgsLat - 35.0);
double radLat = wgsLat / 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);
gcjLat = wgsLat + dLat;
gcjLng = wgsLng + dLon;
}
}
public static void GCJ02_to_WGS84(double gcjLat, double gcjLng, out double wgsLat, out double wgsLng)
{
WGS84_to_GCJ02(gcjLat, gcjLng, out wgsLat, out wgsLng);
wgsLng = gcjLng * 2 - wgsLng;
wgsLat = gcjLat * 2 - wgsLat;
}
private 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;
}
private 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;
}
#endregion
#region 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的互转
public static void GCJ02_to_Bd09(double gcjLat, double gcjLng, out double bdLat, out double bdLng)
{
double z = Math.Sqrt(gcjLng * gcjLng + gcjLat * gcjLat) + 0.00002 * Math.Sin(gcjLat * PI);
double theta = Math.Atan2(gcjLat, gcjLng) + 0.000003 * Math.Cos(gcjLng * PI);
bdLng = z * Math.Cos(theta) + 0.0065;
bdLat = z * Math.Sin(theta) + 0.006;
}
public static void BD09_to_GCJ02(double bdLat, double bdLng, out double gcjLat, out double gcjLng)
{
double x = bdLng - 0.0065, y = bdLat - 0.006;
double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * PI);
double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * PI);
gcjLng = z * Math.Cos(theta);
gcjLat = z * Math.Sin(theta);
}
#endregion
创建头文件,QGCTransform.h:
#ifndef QGCTRANSFORM_H
#define QGCTRANSFORM_H
struct gps_position_t {
int32_t lat; /*< Latitude, expressed as degrees * 1E7*/
int32_t lon; /*< Longitude, expressed as degrees * 1E7*/
double lat_f;
double lon_f;
};
class QGCTransform
{
public:
QGCTransform();
/*WGS-84 to GCJ-02*/
double PI;
double a;
double ee;
gps_position_t gps_t;
void Transform();
void Transform(double lat, double lon);
private:
bool OutOfChina(double lat, double lon);
double TransformLat(double x, double y);
double TransformLon(double x, double y);
};
#endif // QGCTRANSFORM_H
创建源文件,QGCTransform.cpp,转换后的GPS经纬度存储在结构体“gps_t”中,代码如下:
#include "QGCTransform.h"
#include
QGCTransform::QGCTransform(){
PI = 3.14159265358979324;
a = 6378245.0;
ee = 0.00669342162296594323;
}
/*WGS-84 to GCJ-02*/
bool QGCTransform::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;
}
double QGCTransform::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 * qSqrt(qAbs(x));
ret += (20.0 * qSin(6.0 * x * PI) + 20.0 * qSin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * qSin(y * PI) + 40.0 * qSin(y / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * qSin(y / 12.0 * PI) + 320 * qSin(y * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
double QGCTransform::TransformLon(double x, double y)
{
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * qSqrt(qAbs(x));
ret += (20.0 * qSin(6.0 * x * PI) + 20.0 * qSin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * qSin(x * PI) + 40.0 * qSin(x / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * qSin(x / 12.0 * PI) + 300.0 * qSin(x / 30.0 * PI)) * 2.0 / 3.0;
return ret;
}
void QGCTransform::Transform(){
double lat_s=gps_t.lat * 1E-7;
double lon_s=gps_t.lon * 1E-7;
Transform(lat_s, lon_s);
}
void QGCTransform::Transform(double lat_s, double lon_s){
gps_t.lat_f = lat_s;
gps_t.lon_f = lon_s;
gps_t.lat = lat_s * 1E7;
gps_t.lon = lon_s * 1E7;
if (OutOfChina(lat_s, lon_s ))
{
return;
}
double dLat = TransformLat(lon_s - 105.0, lat_s - 35.0);
double dLon = TransformLon(lon_s - 105.0, lat_s - 35.0);
double radLat = lat_s / 180.0 * PI;
double magic = qSin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = qSqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * qCos(radLat) * PI);
gps_t.lat = (lat_s + dLat) * 1E7;
gps_t.lon = (lon_s + dLon) * 1E7;
gps_t.lat_f = lat_s + dLat;
gps_t.lon_f = lon_s + dLon;
}
引用:
1、高德,百度,Google地图定位偏移以及坐标系转换