oc 换算高德经纬度问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

+(double)CalculateTheDistanceWithLon1:(double) lon1
Lat1:(double) lat1
Lon2:(double) lon2
Lat2:(double) lat2{
double er = 6371393.0f;//地球半径
//第一个位置的经纬度
double radlong1 = M_PI*lon1/180.0f;
double radlat1 = M_PI*lat1/180.0f;
//第二个位置的经纬度
double radlat2 = M_PI*lat2/180.0f;
double radlong2 = M_PI*lon2/180.0f;
//判断经纬度的正负
if( radlat1 < 0 ) radlat1 = M_PI/2 + fabs(radlat1);
if( radlat1 > 0 ) radlat1 = M_PI/2 - fabs(radlat1);
if( radlat2 < 0 ) radlat2 = M_PI/2 + fabs(radlat2);
if( radlat2 > 0 ) radlat2 = M_PI/2 - fabs(radlat2);
if( radlong2 < 0 ) radlong2 = M_PI*2 - fabs(radlong2);

double x1 = er * cos(radlong1) * sin(radlat1);
double y1 = er * sin(radlong1) * sin(radlat1);
double z1 = er * cos(radlat1);
double x2 = er * cos(radlong2) * sin(radlat2);
double y2 = er * sin(radlong2) * sin(radlat2);
double z2 = er * cos(radlat2);
double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
double theta = acos((er*er+er*er-d*d)/(2*er*er));

double dist = theta * er;
//返回最终的距离
return dist;
}


static const double a = 6378245.0;
static const double ee = 0.00669342162296594323;
static const double pi = M_PI;
//static const double xPi = M_PI * 3000.0 / 180.0;
+ (CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc {
CLLocationCoordinate2D adjustLoc;
if([self isLocationOutOfChina:wgsLoc]) {
adjustLoc = wgsLoc;
}
else {
double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
long double radLat = wgsLoc.latitude / 180.0 * pi;
long double magic = sin(radLat);
magic = 1 - ee * magic * magic;
long double sqrtMagic = sqrt(magic);
adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
adjustLoc.latitude = wgsLoc.latitude + adjustLat;
adjustLoc.longitude = wgsLoc.longitude + adjustLon;
}
return adjustLoc;
}

+ (double)transformLatWithX:(double)x withY:(double)y {
double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));

lat += (20.0 * sin(6.0 * x * pi) + 20.0 *sin(2.0 * x * pi)) * 2.0 / 3.0;
lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
return lat;
}

+ (double)transformLonWithX:(double)x withY:(double)y {
double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
return lon;
}

+ (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location {
if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271)
return YES;
return NO;
}