系列文章目录
一.gps出来的数据并不能直接使用,需要通过一点点转换后才能
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char lon_str[] = "11329.467276";
char lat_str[] = "2327.041999";
double longitudeToOnenetFormat()
{
double lon_temp = 0;
long lon_Onenet = 0;
int dd_int = 0;
long mm_int = 0;
double lon_Onenet_double = 0;
lon_temp = atof(lon_str);
lon_Onenet =lon_temp*100000; //转换为整数
dd_int = lon_Onenet/10000000; //取出dd
mm_int = lon_Onenet%10000000; //取出MM部分
lon_Onenet_double = dd_int + (double)mm_int/60/100000;//换算为Onenet格式
return lon_Onenet_double;
}
double latitudeToOnenetFormat()
{
double lat_temp = 0;
long lat_Onenet = 0;
int dd_int = 0;
long mm_int = 0;
double lat_Onenet_double = 0;
lat_temp = atof(lat_str);
lat_Onenet =lat_temp*100000; //转换为整数
dd_int = lat_Onenet/10000000; //取出dd
mm_int = lat_Onenet%10000000; //取出MM部分
lat_Onenet_double = dd_int + (double)mm_int/60/100000;//换算为Onenet格式
return lat_Onenet_double;
}
int main()
{
printf("%f\n",longitudeToOnenetFormat());
printf("%f\n",latitudeToOnenetFormat());
return 0;
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.1415926535897932384626
double* BD09ToWGS84(double, double);
double* WGS84ToBD09(double, double);
double* BD09ToGCJ02(double, double);
double* GCJ02ToBD09(double, double);
double* GCJ02ToWGS84(double, double);
double* WGS84ToGCJ02(double, double);
int main(){
double lon = 116.403988;
double lat = 39.914266;
double *p = WGS84ToBD09(lon, lat);
printf("%.10f, %.10f", p[0], p[1]);
}
// (BD09 <=> WGS84) base on (BD09 <=> GCJ02) and (WGS84 <=> GCJ02)
double* BD09ToWGS84(double lon_BD, double lat_BD){
double *GCJ = BD09ToGCJ02(lon_BD, lat_BD);
return GCJ02ToWGS84(GCJ[0], GCJ[1]);
}
double* WGS84ToBD09(double lon_WGS, double lat_WGS){
double *GCJ = WGS84ToGCJ02(lon_WGS, lat_WGS);
return GCJ02ToBD09(GCJ[0], GCJ[1]);
}
// BD09 <=> GCJ02
double baiduFactor = (PI * 3000.0) / 180.0;
double* BD09ToGCJ02(double lon_BD, double lat_BD){
double *GCJ = (double *)malloc(2*sizeof(double));
double x = lon_BD - 0.0065;
double y = lat_BD - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * baiduFactor);
double theta = atan2(y, x) - 0.000003 * cos(x * baiduFactor);
GCJ[0] = z * cos(theta);
GCJ[1] = z * sin(theta);
return GCJ;
}
double* GCJ02ToBD09(double lon_GCJ, double lat_GCJ){
double *BD = (double *)malloc(2*sizeof(double));
double z = sqrt(lon_GCJ * lon_GCJ + lat_GCJ * lat_GCJ) + 0.00002 * sin(lat_GCJ * baiduFactor);
double theta = atan2(lat_GCJ, lon_GCJ) + 0.000003 * cos(lon_GCJ * baiduFactor);
BD[0] = z * cos(theta) + 0.0065;
BD[1] = z * sin(theta) + 0.006;
return BD;
}
// WGS84 <=> GCJ02
double a = 6378245;
double ee = 0.006693421622965823;
// roughly check whether coordinates are in China.
int isInChinaBbox(double lon, double lat){
return lon >= 72.004 && lon <= 137.8347 && lat >= 0.8293 && lat <= 55.8271;
}
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(fabs(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.0 * sin((y * PI) / 30.0)) * 2.0) / 3.0;
return ret;
}
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(fabs(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;
}
double* delta(double lon, double lat){
double *d = (double *)malloc(2*sizeof(double));
double dLon = transformLon(lon - 105.0, lat - 35.0);
double dLat = transformLat(lon - 105.0, lat - 35.0);
double radLat = (lat / 180.0) * PI;
double magic = sin(radLat);
magic = 1.0 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
d[0] = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * PI);
d[1] = (dLat * 180.0) / ((a * (1.0 - ee)) / (magic * sqrtMagic) * PI);
return d;
}
double* WGS84ToGCJ02(double lon_WGS, double lat_WGS){
double *GCJ = (double *)malloc(2*sizeof(double));
if (!isInChinaBbox(lon_WGS, lat_WGS)){
GCJ[0] = lon_WGS;
GCJ[1] = lat_WGS;
return GCJ;
}
double *d = delta(lon_WGS, lat_WGS);
GCJ[0] = lon_WGS + d[0];
GCJ[1] = lat_WGS + d[1];
return GCJ;
}
double* GCJ02ToWGS84(double lon_GCJ, double lat_GCJ) {
double *WGS = (double *)malloc(2*sizeof(double));
if (!isInChinaBbox(lon_GCJ, lat_GCJ)){
WGS[0] = lon_GCJ;
WGS[1] = lat_GCJ;
return WGS;
}
WGS[0] = lon_GCJ; WGS[1] = lat_GCJ;
double *temp = WGS84ToGCJ02(WGS[0], WGS[1]);
double dx = temp[0] - lon_GCJ;
double dy = temp[1] - lat_GCJ;
while (fabs(dx) > 1e-6 || fabs(dy) > 1e-6) {
WGS[0] -= dx;
WGS[1] -= dy;
temp = WGS84ToGCJ02(WGS[0], WGS[1]);
dx = temp[0] - lon_GCJ;
dy = temp[1] - lat_GCJ;
}
return WGS;
}
坐标转换链接: link
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
`
坐标查询网站
http://api.map.baidu.com/lbsapi/getpoint/index.html
整体代码
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.1415926535897932384626
double* BD09ToWGS84(double, double);
double* WGS84ToBD09(double, double);
double* BD09ToGCJ02(double, double);
double* GCJ02ToBD09(double, double);
double* GCJ02ToWGS84(double, double);
double* WGS84ToGCJ02(double, double);
double lon ;
double lat ;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char lon_str[] = "12154.16691545";
char lat_str[] = "3052.37240819";
double longitudeToOnenetFormat()
{
double lon_temp = 0;
long lon_Onenet = 0;
int dd_int = 0;
long mm_int = 0;
double lon_Onenet_double = 0;
lon_temp = atof(lon_str);
lon_Onenet =lon_temp*100000; //转换为整数
dd_int = lon_Onenet/10000000; //取出dd
mm_int = lon_Onenet%10000000; //取出MM部分
lon_Onenet_double = dd_int + (double)mm_int/60/100000;//换算为Onenet格式
return lon_Onenet_double;
}
double latitudeToOnenetFormat()
{
double lat_temp = 0;
long lat_Onenet = 0;
int dd_int = 0;
long mm_int = 0;
double lat_Onenet_double = 0;
lat_temp = atof(lat_str);
lat_Onenet =lat_temp*100000; //转换为整数
dd_int = lat_Onenet/10000000; //取出dd
mm_int = lat_Onenet%10000000; //取出MM部分
lat_Onenet_double = dd_int + (double)mm_int/60/100000;//换算为Onenet格式
return lat_Onenet_double;
}
int main(){
lon=longitudeToOnenetFormat() ;
lat =latitudeToOnenetFormat() ;
double *p = WGS84ToBD09(lon, lat);
printf("%.10f, %.10f", p[0], p[1]);
}
// (BD09 <=> WGS84) base on (BD09 <=> GCJ02) and (WGS84 <=> GCJ02)
double* BD09ToWGS84(double lon_BD, double lat_BD){
double *GCJ = BD09ToGCJ02(lon_BD, lat_BD);
return GCJ02ToWGS84(GCJ[0], GCJ[1]);
}
double* WGS84ToBD09(double lon_WGS, double lat_WGS){
double *GCJ = WGS84ToGCJ02(lon_WGS, lat_WGS);
return GCJ02ToBD09(GCJ[0], GCJ[1]);
}
// BD09 <=> GCJ02
double baiduFactor = (PI * 3000.0) / 180.0;
double* BD09ToGCJ02(double lon_BD, double lat_BD){
double *GCJ = (double *)malloc(2*sizeof(double));
double x = lon_BD - 0.0065;
double y = lat_BD - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * baiduFactor);
double theta = atan2(y, x) - 0.000003 * cos(x * baiduFactor);
GCJ[0] = z * cos(theta);
GCJ[1] = z * sin(theta);
return GCJ;
}
double* GCJ02ToBD09(double lon_GCJ, double lat_GCJ){
double *BD = (double *)malloc(2*sizeof(double));
double z = sqrt(lon_GCJ * lon_GCJ + lat_GCJ * lat_GCJ) + 0.00002 * sin(lat_GCJ * baiduFactor);
double theta = atan2(lat_GCJ, lon_GCJ) + 0.000003 * cos(lon_GCJ * baiduFactor);
BD[0] = z * cos(theta) + 0.0065;
BD[1] = z * sin(theta) + 0.006;
return BD;
}
// WGS84 <=> GCJ02
double a = 6378245;
double ee = 0.006693421622965823;
// roughly check whether coordinates are in China.
int isInChinaBbox(double lon, double lat){
return lon >= 72.004 && lon <= 137.8347 && lat >= 0.8293 && lat <= 55.8271;
}
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(fabs(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.0 * sin((y * PI) / 30.0)) * 2.0) / 3.0;
return ret;
}
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(fabs(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;
}
double* delta(double lon, double lat){
double *d = (double *)malloc(2*sizeof(double));
double dLon = transformLon(lon - 105.0, lat - 35.0);
double dLat = transformLat(lon - 105.0, lat - 35.0);
double radLat = (lat / 180.0) * PI;
double magic = sin(radLat);
magic = 1.0 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
d[0] = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * PI);
d[1] = (dLat * 180.0) / ((a * (1.0 - ee)) / (magic * sqrtMagic) * PI);
return d;
}
double* WGS84ToGCJ02(double lon_WGS, double lat_WGS){
double *GCJ = (double *)malloc(2*sizeof(double));
if (!isInChinaBbox(lon_WGS, lat_WGS)){
GCJ[0] = lon_WGS;
GCJ[1] = lat_WGS;
return GCJ;
}
double *d = delta(lon_WGS, lat_WGS);
GCJ[0] = lon_WGS + d[0];
GCJ[1] = lat_WGS + d[1];
return GCJ;
}
double* GCJ02ToWGS84(double lon_GCJ, double lat_GCJ) {
double *WGS = (double *)malloc(2*sizeof(double));
if (!isInChinaBbox(lon_GCJ, lat_GCJ)){
WGS[0] = lon_GCJ;
WGS[1] = lat_GCJ;
return WGS;
}
WGS[0] = lon_GCJ; WGS[1] = lat_GCJ;
double *temp = WGS84ToGCJ02(WGS[0], WGS[1]);
double dx = temp[0] - lon_GCJ;
double dy = temp[1] - lat_GCJ;
while (fabs(dx) > 1e-6 || fabs(dy) > 1e-6) {
WGS[0] -= dx;
WGS[1] -= dy;
temp = WGS84ToGCJ02(WGS[0], WGS[1]);
dx = temp[0] - lon_GCJ;
dy = temp[1] - lat_GCJ;
}
return WGS;
}
2.判断两GPS距离跟方位
#define _PI 3.1415926
#define PI_180 0.01745329 //pi/180 角度转弧度
#define Earth_R 6372.795477598
void getDistanceBearing(double StartLat,double StartLng,double EndLat,double EndLng,double* dis,double* bearing)
{
//Fai对应纬度 Lat,temp对应经度 lon
double Fai1 = StartLat * PI_180;
double Fai2 = EndLat * PI_180;
double temp = (EndLng - StartLng) * PI_180;
*dis = Earth_R * acos(sin(Fai1) * sin(Fai2) + cos(Fai1)*cos(Fai2)*cos(temp)) * 1000;//已经换算成m了
*bearing = atan2(sin(temp)*cos(Fai2),cos(Fai1)*sin(Fai2)-sin(Fai1)*cos(Fai2)*cos(temp))/PI_180;//弧度
if(*bearing < 0)
*bearing += 360;
}