当前位置:网站首页>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
边栏推荐
- [function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
- GPS坐标转百度地图坐标的方法
- 从小数据量分库分表 MySQL 合并迁移数据到 TiDB
- Analysis of Clickhouse mergetree principle
- Ensemble, série shuishu] jour 9
- Pytorch builds the simplest version of neural network
- Kubernetes notes (IV) kubernetes network
- Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
- 为什么网站打开速度慢?
- Get a screenshot of a uiscrollview, including off screen parts
猜你喜欢
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Oauth2.0 - using JWT to replace token and JWT content enhancement
The most responsible command line beautification tutorial
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
[teacher Zhao Yuqiang] redis's slow query log
Configure DTD of XML file
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
[teacher Zhao Yuqiang] MySQL flashback
Solve the problem of automatic disconnection of SecureCRT timeout connection
Together, Shangshui Shuo series] day 9
随机推荐
Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
Kubernetes notes (V) configuration management
[Shangshui Shuo series together] day 10
Btrfs and ext4 - features, strengths and weaknesses
pytorch 搭建神经网络最简版
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
Es 2022 officially released! What are the new features?
Installation du plug - in CAD et chargement automatique DLL, Arx
[teacher Zhao Yuqiang] MySQL high availability architecture: MHA
GPS坐标转百度地图坐标的方法
1. 兩數之和
Analysis of the example of network subnet division in secondary vocational school
Simple handwritten ORM framework
Loss function in pytorch multi classification
Using the ethtool command by example
Kubernetes notes (VII) kuberetes scheduling
Jedis source code analysis (I): jedis introduction, jedis module source code analysis
Ext4 vs XFS -- which file system should you use
Virtual memory technology sharing
Leetcode problem solving summary, constantly updating!