iOS WGS84, GCJ-02, BD-09 solutions on maps

Source: Internet
Author: User
Tags cos

The latest developments in the project include the map sharing module, the Android colleagues group started, using the Baidu Map SDK, I started back, using the iOS SDK Mapkit to do, after that, the problem is, with latitude and longitude coordinates iOS and Android side has a relatively large deviation. I checked the information. Apple Maps in the mainland of the data source is high, check the use of GCJ-02, Baidu Map SDK used is BD-09. Just write a class in the send and receive a good conversion, a little egg pain. Someone on GitHub wrote a ready-made conversion class that was able to refer to the portal. The 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 Bureau of Geographical Coordinates (GCJ-02) < Mars coordinates > * # # # #仅仅在中国大陆的范围的坐标有效, return directly to world standard coordinates * * @param Location World Standard Geographical coordinates (WGS-84) * * @return China National Bureau of Measurement Geographical coordinates (GCJ-02) < Mars coordinates > */+ (cllocationcoordinate2d) wgs84togcj02: ( CLLOCATIONCOORDINATE2D) location;/** * @brief China National Bureau of Measurement Geographical coordinates (GCJ-02) into the world standard Geographical coordinates (WGS-84) * # # # #此接口有1-2 meters or so error, need to pinpoint the situation carefully use * * @param location China National Measurement 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 Bureau of Geographic Coordinates (GCJ-02) < Mars coordinates > convert to Baidu Geographical coordinates (BD-09) * @param location China National Bureau of Measurement 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 Bureau of Geographical Coordinates (GCJ-02) < Mars coordinates > * * @param location Baidu Geographical coordinates (BD-09) * * @return China National Bureau of 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 Example:

    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

Overall, there is a bit of deviation, but compared with the effect before processing. are relatively receptive to some.


References:

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

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

iOS WGS84, GCJ-02, BD-09 solutions on maps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.