本文引用地址
一、地图坐标系解释
在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系:
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)。
二、坐标系转换(C#版本)
C#版本的WGS-84坐标系转为GCJ-02坐标系,(亲测可用):
const double PI = 3.14159265358979324;const double a = 6378245.0;const double ee = 0.00669342162296594323;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;}
三、QT/C++版本的坐标转换
创建头文件,QGCTransform.h:
#ifndef QGCTRANSFORM_H#define QGCTRANSFORM_Hstruct 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 <QtMath>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;