当前位置:网站首页>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
边栏推荐
- Redhat7 system root user password cracking
- What's the difference between using the Service Worker Cache API and regular browser cache?
- BeanDefinitionRegistryPostProcessor
- It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
- Oauth2.0 - explanation of simplified mode, password mode and client mode
- pytorch DataLoader实现miniBatch(未完成)
- Detailed explanation of iptables (1): iptables concept
- [explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
- [teacher Zhao Yuqiang] kubernetes' probe
- QT read write excel -- qxlsx insert chart 5
猜你喜欢

Kubernetes notes (IV) kubernetes network

卷积神经网络CNN中的卷积操作详解
![[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence

Apache+php+mysql environment construction is super detailed!!!

Redhat7系统root用户密码破解

SVN分支管理

Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
![[branch and cycle] | | super long detailed explanation + code analysis + a trick game](/img/aa/543d4f0dcbcd664be963579af77ec9.jpg)
[branch and cycle] | | super long detailed explanation + code analysis + a trick game

QT read write excel -- qxlsx insert chart 5

Maximum likelihood estimation, divergence, cross entropy
随机推荐
智牛股项目--04
Why is the website slow to open?
The server data is all gone! Thinking caused by a RAID5 crash
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
PHP notes are super detailed!!!
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
1. 两数之和
Configure DTD of XML file
Leetcode problem solving summary, constantly updating!
Code generator - single table query crud - generator
How to create your own repository for software packages on Debian
智牛股--03
[teacher Zhao Yuqiang] index in mongodb (Part 2)
Kubernetes notes (II) pod usage notes
理解 YOLOV1 第一篇 预测阶段
BeanDefinitionRegistryPostProcessor
1. Somme des deux nombres
chromedriver对应版本下载
卷积神经网络CNN中的卷积操作详解
Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver