iOS 百度与高德地图坐标的互相转换

 1 //
 2 //  JZLocationConverter.h
 3 //  JZCLLocationMangerDome
 4 //
 5 //  Created by jack zhou on 13-8-22.
 6 //  Copyright (c) 2013年 JZ. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import <CoreLocation/CoreLocation.h>
11 @interface JZLocationConverter : NSObject
12 
13 /**
14  *    @brief    世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
15  *
16  *  ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
17  *
18  *    @param     location     世界标准地理坐标(WGS-84)
19  *
20  *    @return    中国国测局地理坐标(GCJ-02)<火星坐标>
21  */
22 + (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location;
23 
24 
25 /**
26  *    @brief    中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
27  *
28  *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
29  *
30  *    @param     location     中国国测局地理坐标(GCJ-02)
31  *
32  *    @return    世界标准地理坐标(WGS-84)
33  */
34 + (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location;
35 
36 
37 /**
38  *    @brief    世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
39  *
40  *    @param     location     世界标准地理坐标(WGS-84)
41  *
42  *    @return    百度地理坐标(BD-09)
43  */
44 + (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location;
45 
46 
47 /**
48  *    @brief    中国国测局地理坐标(GCJ-02)<火星坐标> 转换成 百度地理坐标(BD-09)
49  *
50  *    @param     location     中国国测局地理坐标(GCJ-02)<火星坐标>
51  *
52  *    @return    百度地理坐标(BD-09)
53  */
54 + (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location;
55 
56 
57 /**
58  *    @brief    百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
59  *
60  *    @param     location     百度地理坐标(BD-09)
61  *
62  *    @return    中国国测局地理坐标(GCJ-02)<火星坐标>
63  */
64 + (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location;
65 
66 
67 /**
68  *    @brief    百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
69  *
70  *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
71  *
72  *    @param     location     百度地理坐标(BD-09)
73  *
74  *    @return    世界标准地理坐标(WGS-84)
75  */
76 + (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location;
77 
78 
79 @end
  1 //
  2 //  JZLocationConverter.m
  3 //  JZCLLocationMangerDome
  4 //
  5 //  Created by jack zhou on 13-8-22.
  6 //  Copyright (c) 2013年 JZ. All rights reserved.
  7 //
  8 
  9 #import "JZLocationConverter.h"
 10 #import <CoreLocation/CoreLocation.h>
 11 #define LAT_OFFSET_0(x,y) -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x))
 12 #define LAT_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
 13 #define LAT_OFFSET_2 (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0
 14 #define LAT_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0
 15 
 16 #define LON_OFFSET_0(x,y) 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x))
 17 #define LON_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
 18 #define LON_OFFSET_2 (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0
 19 #define LON_OFFSET_3 (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0
 20 
 21 #define RANGE_LON_MAX 137.8347
 22 #define RANGE_LON_MIN 72.004
 23 #define RANGE_LAT_MAX 55.8271
 24 #define RANGE_LAT_MIN 0.8293
 25 // jzA = 6378245.0, 1/f = 298.3
 26 // b = a * (1 - f)
 27 // ee = (a^2 - b^2) / a^2;
 28 #define jzA 6378245.0
 29 #define jzEE 0.00669342162296594323
 30 
 31 
 32 
 33 @implementation JZLocationConverter
 34 
 35 + (double)transformLat:(double)x bdLon:(double)y
 36 {
 37     double ret = LAT_OFFSET_0(x, y);
 38     ret += LAT_OFFSET_1;
 39     ret += LAT_OFFSET_2;
 40     ret += LAT_OFFSET_3;
 41     return ret;
 42 }
 43 
 44 + (double)transformLon:(double)x bdLon:(double)y
 45 {
 46     double ret = LON_OFFSET_0(x, y);
 47     ret += LON_OFFSET_1;
 48     ret += LON_OFFSET_2;
 49     ret += LON_OFFSET_3;
 50     return ret;
 51 }
 52 
 53 + (BOOL)outOfChina:(double)lat bdLon:(double)lon
 54 {
 55     if (lon < RANGE_LON_MIN || lon > RANGE_LON_MAX)
 56         return true;
 57     if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
 58         return true;
 59     return false;
 60 }
 61 
 62 + (CLLocationCoordinate2D)gcj02Encrypt:(double)ggLat bdLon:(double)ggLon
 63 {
 64     CLLocationCoordinate2D resPoint;
 65     double mgLat;
 66     double mgLon;
 67     if ([self outOfChina:ggLat bdLon:ggLon]) {
 68         resPoint.latitude = ggLat;
 69         resPoint.longitude = ggLon;
 70         return resPoint;
 71     }
 72     double dLat = [self transformLat:(ggLon - 105.0)bdLon:(ggLat - 35.0)];
 73     double dLon = [self transformLon:(ggLon - 105.0) bdLon:(ggLat - 35.0)];
 74     double radLat = ggLat / 180.0 * M_PI;
 75     double magic = sin(radLat);
 76     magic = 1 - jzEE * magic * magic;
 77     double sqrtMagic = sqrt(magic);
 78     dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * M_PI);
 79     dLon = (dLon * 180.0) / (jzA / sqrtMagic * cos(radLat) * M_PI);
 80     mgLat = ggLat + dLat;
 81     mgLon = ggLon + dLon;
 82     
 83     resPoint.latitude = mgLat;
 84     resPoint.longitude = mgLon;
 85     return resPoint;
 86 }
 87 
 88 + (CLLocationCoordinate2D)gcj02Decrypt:(double)gjLat gjLon:(double)gjLon {
 89     CLLocationCoordinate2D  gPt = [self gcj02Encrypt:gjLat bdLon:gjLon];
 90     double dLon = gPt.longitude - gjLon;
 91     double dLat = gPt.latitude - gjLat;
 92     CLLocationCoordinate2D pt;
 93     pt.latitude = gjLat - dLat;
 94     pt.longitude = gjLon - dLon;
 95     return pt;
 96 }
 97 
 98 + (CLLocationCoordinate2D)bd09Decrypt:(double)bdLat bdLon:(double)bdLon
 99 {
100     CLLocationCoordinate2D gcjPt;
101     double x = bdLon - 0.0065, y = bdLat - 0.006;
102     double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);
103     double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);
104     gcjPt.longitude = z * cos(theta);
105     gcjPt.latitude = z * sin(theta);
106     return gcjPt;
107 }
108 
109 +(CLLocationCoordinate2D)bd09Encrypt:(double)ggLat bdLon:(double)ggLon
110 {
111     CLLocationCoordinate2D bdPt;
112     double x = ggLon, y = ggLat;
113     double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);
114     double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);
115     bdPt.longitude = z * cos(theta) + 0.0065;
116     bdPt.latitude = z * sin(theta) + 0.006;
117     return bdPt;
118 }
119 
120 
121 + (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location
122 {
123     return [self gcj02Encrypt:location.latitude bdLon:location.longitude];
124 }
125 
126 + (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location
127 {
128     return [self gcj02Decrypt:location.latitude gjLon:location.longitude];
129 }
130 
131 
132 + (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location
133 {
134     CLLocationCoordinate2D gcj02Pt = [self gcj02Encrypt:location.latitude
135                                                   bdLon:location.longitude];
136     return [self bd09Encrypt:gcj02Pt.latitude bdLon:gcj02Pt.longitude] ;
137 }
138 
139 + (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location
140 {
141     return  [self bd09Encrypt:location.latitude bdLon:location.longitude];
142 }
143 
144 + (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location
145 {
146     return [self bd09Decrypt:location.latitude bdLon:location.longitude];
147 }
148 
149 + (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location
150 {
151     CLLocationCoordinate2D gcj02 = [self bd09ToGcj02:location];
152     return [self gcj02Decrypt:gcj02.latitude gjLon:gcj02.longitude];
153 }
154 
155 @end

 源代码地址:https://github.com/JackZhouCn/JZLocationConverter

我采用的方法  + (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location 把百度API坐标转换之后调用高德地图导航

 

下面是朋友提供的api  高德转百度  如果想百度转高德 直接对方法进行逆反操作即可

+ (CLLocation *)AMapLocationFromBaiduLocation:(CLLocation *)BaiduLocation;
{
const double x_pi = M_PI * 3000.0 / 180.0;
double x = BaiduLocation.coordinate.longitude - 0.0065, y = BaiduLocation.coordinate.latitude - 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);
double AMapLongitude = z * cos(theta);
double AMapLatitude = z * sin(theta);
CLLocation *AMapLocation = [[CLLocation alloc] initWithLatitude:AMapLatitude longitude:AMapLongitude];
return AMapLocation;
}

posted @ 2016-10-27 14:38  天生的普通人  阅读(4637)  评论(0编辑  收藏  举报