当前位置:网站首页>Method of converting GPS coordinates to Baidu map coordinates
Method of converting GPS coordinates to Baidu map coordinates
2022-07-03 06:03:00 【UU_ Yang】
from :GPS The method of converting coordinates to Baidu map coordinates - Programmer base
First of all, I need to know GPS The coordinate system of .GPS Coordinate system follows WGS-84 standard , By this standard ,GPS The chip can send different packet formats . According to the difference of its data frame header ,GPS The data can be classified as GPGGA、GPGSA、GPGSV、GPRMC etc. . These frame headers identify the composition of subsequent intra frame data . Usually , The positioning data we are concerned about, such as longitude and latitude 、 Speed 、 Time, etc. can be from GPRMC Get in the frame .
I won't explain the format of specific frames this time , You can easily find it on the Internet , The format of data frame can also be found in the interface document of the general chip . By reading GPRMC package , We can get something similar to “3040.8639,N,10405.7573,E” The location data of , Its field meaning can be found in the document . The result of my analysis here is : North latitude 30°40.8639', East longitude 104°5.7573'.
After getting these two data , We can try to locate it on the map , We found that most map locations use decimal coordinates . Here we need to talk about how to put a GPS Raw data , Data converted to decimal form .
give an example :106°14'15" Transformation
Because degrees, minutes and seconds are in hexadecimal
So it can be converted in this way :
15/60=0.25 branch
(14+0.25)/60=0.2375 degree
106+0.2375=106.2375 degree
So the end result is 106.2375°
After this conversion , My position coordinates 【 North latitude 30°40.8639', East longitude 104°5.7573'】 It can become 【30.681065N,104.095955E】( This is WGS-84 Coordinates of ) This look . This coordinate is GPS Physical positioning of , According to international standards , This coordinate needs to be GCJ-02 Offset conversion , The converted coordinates can be in google Map 、 Gould map 、 And positioning on Tencent map ( The above three companies follow GCJ-02 encryption ). As for why this should be done , It's because the coordinates are encrypted . because GCJ-02 It is an irreversible transformation .
You zhuxun is posted here WGS-84 The standard is converted to GCJ-02 Of C++ Source code :
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;
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 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * pi) + 320 * 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 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
/*
Parameters
wgLat:WGS-84 latitude wgLon:WGS-84 longitude
Return value :
mgLat:GCJ-02 latitude mgLon:GCJ-02 longitude
*/
void gps_transform( double wgLat, double wgLon, double& mgLat, 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 = sin(radLat);
magic = 1 - ee * magic * magic; double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
mgLat = wgLat + dLat; mgLon = wgLon + dLon;
}
After the above conversion , Our coordinates can be followed GCJ-02 The standard map has been deleted , Here is a more useful test Location URL :http://www.gpsspg.com/maps.htm
Baidu map is a little special , It's in GCJ-02 On this basis, another encryption is carried out , Baidu calls this encryption standard BD-09, But this encryption is not publicly available . But the omnipotent network and omnipotent netizens will always have a way to solve this kind of problem . Here I will post a feasible conversion method , After testing , The positioning is very accurate .
#include <math.h>
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
// take GCJ-02 The coordinates are converted to BD-09 coordinate
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;
}
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);
}
such , Our coordinates can also be accurately located on Baidu map , They were filled with joy ~~
In addition, a legend that can easily explain why coordinates need to be converted is pasted , So that we can understand :
International conversion practice :
Baidu's approach :
It also explains , Why? GPS The reason why coordinates need to be converted . Thanks to the people who contributed these materials to the Internet !
Reference resources :
http://m.blog.csdn.net/blog/wildboy2001/39497681
http://www.haodaima.net/art/2441684
http://www.gpsspg.com/maps.htm
http://wenda.haosou.com/q/1365472754066079
http://www.geekcome.com/content-10-1464-1.html
边栏推荐
- 项目总结--04
- Deep learning, thinking from one dimensional input to multi-dimensional feature input
- phpstudy设置项目可以由局域网的其他电脑可以访问
- Disruptor learning notes: basic use, core concepts and principles
- [advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
- Kubernetes notes (II) pod usage notes
- BeanDefinitionRegistryPostProcessor
- Detailed explanation of iptables (1): iptables concept
- Clickhouse learning notes (2): execution plan, table creation optimization, syntax optimization rules, query optimization, data consistency
- [teacher Zhao Yuqiang] RDB persistence of redis
猜你喜欢
[teacher Zhao Yuqiang] MySQL flashback
GPS坐标转百度地图坐标的方法
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
Kubernetes resource object introduction and common commands (V) - (configmap)
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
PHP notes are super detailed!!!
Alibaba cloud OOS file upload
2022.DAY592
Qt读写Excel--QXlsx插入图表5
为什么网站打开速度慢?
随机推荐
Understand expectations (mean / estimate) and variances
Deep learning, thinking from one dimensional input to multi-dimensional feature input
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
[teacher Zhao Yuqiang] use Oracle's tracking file
Solve the problem that Anaconda environment cannot be accessed in PowerShell
Kubernetes cluster environment construction & Deployment dashboard
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
Common exceptions when Jenkins is released (continuous update...)
pytorch 多分类中的损失函数
tabbar的设置
一起上水碩系列】Day 9
从 Amazon Aurora 迁移数据到 TiDB
Alibaba cloud Alipay sandbox payment
为什么网站打开速度慢?
pytorch DataLoader实现miniBatch(未完成)
Yum is too slow to bear? That's because you didn't do it
Troubleshooting of 32GB Jetson Orin SOM failure to brush
Bio, NiO, AIO details