当前位置:网站首页>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
边栏推荐
- Download the corresponding version of chromedriver
- Use telnet to check whether the port corresponding to the IP is open
- BeanDefinitionRegistryPostProcessor
- Code generator - single table query crud - generator
- [video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
- 理解 YOLOV1 第一篇 预测阶段
- [Zhao Yuqiang] deploy kubernetes cluster with binary package
- The server data is all gone! Thinking caused by a RAID5 crash
- How does win7 solve the problem that telnet is not an internal or external command
- Detailed explanation of findloadedclass
猜你喜欢
![[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis](/img/df/884313a69fb1e613aec3497800f7ba.jpg)
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
![[function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram](/img/29/1644588927226a49d4b8815d8bc196.jpg)
[function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram

Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
![[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb

Redis cannot connect remotely.

Method of finding prime number
![Together, Shangshui Shuo series] day 9](/img/39/c1ba1bac82b0ed110f36423263ffd0.png)
Together, Shangshui Shuo series] day 9
![Ensemble, série shuishu] jour 9](/img/39/c1ba1bac82b0ed110f36423263ffd0.png)
Ensemble, série shuishu] jour 9

Bio, NiO, AIO details

从小数据量 MySQL 迁移数据到 TiDB
随机推荐
The programmer shell with a monthly salary of more than 10000 becomes a grammar skill for secondary school. Do you often use it!!!
Analysis of the example of network subnet division in secondary vocational school
伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)
Apple submitted the new MAC model to the regulatory database before the spring conference
CAD插件的安装和自动加载dll、arx
Clickhouse learning notes (2): execution plan, table creation optimization, syntax optimization rules, query optimization, data consistency
[teacher Zhao Yuqiang] use the catalog database of Oracle
Together, Shangshui Shuo series] day 9
Core principles and source code analysis of disruptor
Oauth2.0 - using JWT to replace token and JWT content enhancement
从 Amazon Aurora 迁移数据到 TiDB
How to create and configure ZABBIX
[advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
Apache+php+mysql environment construction is super detailed!!!
What's the difference between using the Service Worker Cache API and regular browser cache?
Kubernetes notes (10) kubernetes Monitoring & debugging
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
Qt读写Excel--QXlsx插入图表5
@Import annotation: four ways to import configuration classes & source code analysis
Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)