### 一、地图坐标系解释

3. 百度坐标系：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;
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++版本的坐标转换

#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


#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;
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;


05-10 6万+

06-28 948
03-27 1783
05-22 2万+
12-27 6536
03-18 1万+
07-19 2万+
07-19 1万+
11-10 1941
04-19