Recently developed projects involving map location sharing module, the Android group of colleagues first started, with the Baidu Map SDK, I started behind, with the iOS SDK Mapkit do, after the problem came, the same latitude and longitude coordinates in the iOS and Android side appeared a relatively large deviation. I checked the information. Apple Maps in the mainland of the data source is gold, check the following the use of GCJ-02, Baidu Map SDK is BD-09, had to write a class in the send and receive a good conversion, a little egg pain. Someone on GitHub wrote a ready-made conversion class, which can refer to the reference portal, and its main code is shown below:
Header file:
#import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface jzlocationconverter: nsobject/** * @brief World Standard Geographical coordinates (WGS-84) Convert to China National Survey Bureau Geographical coordinates (GCJ-02) < Mars coordinates > * # # # #只在中国大陆的范围的坐标有效, return directly to world standard coordinates * * @param Location World Standard Geographical coordinates (WGS-84) * * @return China National Survey Bureau Geographical coordinates (GCJ-02) < Mars coordinates > */+ (cllocationcoordinate2d) wgs84togcj02: ( CLLOCATIONCOORDINATE2D) location;/** * @brief China National Survey Bureau Geographical coordinates (GCJ-02) into the world standard Geographical coordinates (WGS-84) * # # # #此接口有1-2 meters of error, need to pinpoint the situation with caution * * @param location China National Survey Bureau Geographical Coordinates (GCJ-02) * * @return World Standard Geographical coordinates (WGS-84) */+ (cllocationcoordinate2d) GCJ02TOWGS84: ( CLLOCATIONCOORDINATE2D) location;/** * @brief World Standard Geographical coordinates (WGS-84) converted to Baidu geographical coordinates (BD-09) * @param location World Standard Geographical coordinates (WGS-84) * *@ Return Baidu Geographical coordinates (BD-09) */+ (cllocationcoordinate2d) wgs84tobd09: (cllocationcoordinate2d) location;/** *@ Brief China National Survey Bureau Geographical coordinates (GCJ-02) < Mars coordinates > convert to Baidu Geographical coordinates (BD-09) * * @param location China National Survey Bureau Geographical coordinates (GCJ-02) < Mars coordinates > * *@ Return Baidu Geographical coordinates (BD-09) */+ (cllocationcoordinate2d) gcj02tobd09: (cllocationcoordinate2d) location;/** *@ Brief Baidu geographical coordinates (BD-09) Convert to China National Survey Bureau Geographical coordinates (GCJ-02) < Mars coordinates > * * @param location Baidu Geographical coordinates (BD-09) * * @return China National Survey Bureau Geographical coordinates (GCJ-02) < Mars coordinates > */+ ( CLLOCATIONCOORDINATE2D) bd09togcj02: (cllocationcoordinate2d) location;/** * @brief Baidu Geographical coordinates (BD-09) converted to world standard geographical coordinates (WGS-84) * # # # #此接口有1-About 2 meters of error, need to pinpoint the situation with caution * * @param location Baidu Geographical coordinates (BD-09) * * @return World Standard Geographical coordinates (WGS-84) */+ (cllocationcoordinate2d ) BD09TOWGS84: (cllocationcoordinate2d) location; @end
Implementation file
#import "JZLocationConverter.h" #import <CoreLocation/CoreLocation.h> #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)) #define LAT_OFFSET_1 (20.0 * SIN (6.0 * x * M_PI) + 20.0 * SIN (2.0 * x * m_pi)) * 2.0/3.0#define lat_offset_2 (20.0 * sin (y * m_pi) + 40.0 * sin (y/3.0 * m_pi)) * 2.0/3.0#define Lat_o Ffset_3 (160.0 * sin (y/12.0 * m_pi) + * * sin (y * m_pi/30.0)) * 2.0/3.0#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)) #define LON_OFFSET_1 (20.0 * SIN (6.0 * x * M_PI) + 20.0 * SIN (2.0 * x) * m_pi) * 2.0/3.0#define lon_offset_2 (20.0 * sin (x * m_pi) + 40.0 * sin (x/3.0 * m_pi)) * 2.0/3.0#define Lon_offset _3 (150.0 * sin (x/12.0 * m_pi) + 300.0 * sin (x/30.0 * m_pi)) * 2.0/3.0#define Range_lon_max 137.8347#define Range_lo N_min 72.004#define range_lat_max 55.8271#define range_lat_min 0.8293//Jza = 6378245.0, F = 298.3//B = A * (1-F)//E E = (a^2-b^2)/a^2; #defiNe jza 6378245.0#define jzee 0.00669342162296594323@implementation jzlocationconverter+ (Double) TransformLat: (Double ) x Bdlon: (double) y{double ret = lat_offset_0 (x, y); RET + = lat_offset_1; RET + = lat_offset_2; RET + = Lat_offset_3; return ret;} + (Double) Transformlon: (double) x Bdlon: (double) y{double ret = lon_offset_0 (x, y); RET + = lon_offset_1; RET + = lon_offset_2; RET + = Lon_offset_3; return ret;} + (BOOL) Outofchina: (double) lat Bdlon: (double) lon{if (Lon < Range_lon_min | | lon > RANGE_LON_MAX) return t Rue if (Lat < range_lat_min | | lat > Range_lat_max) return true; return false;} + (Cllocationcoordinate2d) Gcj02encrypt: (double) Gglat Bdlon: (double) gglon{cllocationcoordinate2d respoint; Double Mglat; Double Mglon; if ([self Outofchina:gglat Bdlon:gglon]) {respoint.latitude = Gglat; Respoint.longitude = Gglon; return respoint; } Double Dlat = [self Transformlat: (ggLon-105.0) Bdlon: (ggLat-35.0)]; Double Dlon = [self Transformlon: (ggLon-105.0) Bdlon: (ggLat-35.0)]; Double Radlat = gglat/180.0 * M_PI; Double magic = sin (Radlat); Magic = 1-jzee * Magic * MAGIC; Double sqrtmagic = sqrt (Magic); Dlat = (Dlat * 180.0)/((Jza * (1-jzee))/(Magic * sqrtmagic) * m_pi); Dlon = (Dlon * 180.0)/(jza/sqrtmagic * cos (radlat) * m_pi); Mglat = Gglat + Dlat; Mglon = Gglon + Dlon; Respoint.latitude = Mglat; Respoint.longitude = Mglon; return respoint;} + (Cllocationcoordinate2d) Gcj02decrypt: (double) Gjlat Gjlon: (double) Gjlon {cllocationcoordinate2d gPt = [Self Gcj02enc Rypt:gjlat Bdlon:gjlon]; Double Dlon = Gpt.longitude-gjlon; Double Dlat = Gpt.latitude-gjlat; Cllocationcoordinate2d pt; Pt.latitude = Gjlat-dlat; Pt.longitude = Gjlon-dlon; Return PT;} + (Cllocationcoordinate2d) Bd09decrypt: (double) Bdlat Bdlon: (double) bdlon{cllocationcoordinate2d gcjpt; Double x = bdLon-0.0065, y = bdLat-0.006; Double z = sqrt (x * x + y * y)-0.00002 * sin (y * m_pi); Double theta = atan2 (y, x)-0.000003 * cos (x * m_pi); Gcjpt.longitude = z * cos (theta); Gcjpt.latitude = z * sin (theta); return gcjpt;} + (Cllocationcoordinate2d) Bd09encrypt: (double) Gglat Bdlon: (double) gglon{cllocationcoordinate2d bdpt; Double x = gglon, y = Gglat; Double z = sqrt (x * x + y * y) + 0.00002 * sin (y * m_pi); Double theta = atan2 (y, x) + 0.000003 * cos (x * m_pi); Bdpt.longitude = z * cos (theta) + 0.0065; Bdpt.latitude = z * sin (theta) + 0.006; return BDPT;} + (Cllocationcoordinate2d) wgs84togcj02: (cllocationcoordinate2d) location{return [self gcj02encrypt: Location.latitude bdLon:location.longitude];} + (Cllocationcoordinate2d) gcj02towgs84: (cllocationcoordinate2d) location{return [self gcj02decrypt: Location.latitude gjLon:location.longitude];} + (Cllocationcoordinate2d) wgs84tobd09: (cllocationcoordinate2d) location{cllocationcoordinate2d gcj02Pt = [self Gcj02encrypt:location.Latitude BdLon:location.longitude]; return [self bd09Encrypt:gcj02Pt.latitude bdLon:gcj02Pt.longitude];} + (Cllocationcoordinate2d) gcj02tobd09: (cllocationcoordinate2d) location{return [self bd09Encrypt:location.latitude BdLon:location.longitude];} + (Cllocationcoordinate2d) bd09togcj02: (cllocationcoordinate2d) location{return [self bd09Decrypt:location.latitude BdLon:location.longitude];} + (Cllocationcoordinate2d) bd09towgs84: (cllocationcoordinate2d) location{cllocationcoordinate2d gcj02 = [self Bd09togcj02:location]; return [self gcj02Decrypt:gcj02.latitude gjLon:gcj02.longitude];} @end
Test Case:
Cllocationcoordinate2d gcj02 = Cllocationcoordinate2dmake (114.21892734521,29.575429778924); Cllocationcoordinate2d bd09 = [Jzlocationconverter gcj02tobd09:gcj02]; NSLog (@ "%f,%f", Bd09.latitude, bd09.longitude); Http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition //jzlocationconverter test data: 114.21892734521,29.575429778924 ; Conversion results: 114.224960,29.581853 //Baidu API test data: 114.21892734521,29.575429778924 . Baidu API Conversion Results: 114.22539195429,29.581585367458
Generally speaking, there is a little deviation, but compared with the effect before processing, relatively acceptable.
Reference:
http://blog.csdn.net/jiajiayouba/article/details/25140967
Http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition
http://blog.csdn.net/winnyrain/article/details/22233559
Http://www.kuaifenxiang.net/article/17
http://blog.csdn.net/coolypf/article/details/8569813
Http://www.cppblog.com/socketref/archive/2011/06/29/149713.html
WGS84, GCJ-02, BD-09 solutions on iOS maps