iOS开发实战-地图篇

在iOS项目中使用地图时,有时候会令人相当疑惑:使用代码显示经纬度,有时候距离真实位置相差甚远。要理解其中的原由,还得从国内外经纬度采用的标准开始说起。



首先推荐一个网站:http://www.gpsspg.com/maps.htm,可以搜索经纬度,也可以显示某个位置的经纬度。

1.国内外标准:

国际经纬度坐标标准为 WGS-84(国际标准);国内必须至少使用国测局制定的 GCJ-02(火星坐标),对地理位置进行首次加密。

2.地图产品采用的标准:

地图 标准
国际标准 WGS84
MKMapView(大陆) GCJ-02(火星坐标)
百度地图 BD-09(百度坐标)
高德地图 GCJ-02(火星坐标)
腾讯搜搜地图 GCJ-02(火星坐标)
阿里云地图 GCJ-02(火星坐标)
灵图51ditu地图 GCJ-02(火星坐标)
搜狐搜狗地图 搜狗坐标
图吧MapBar地图 图吧坐标

3.苹果地图(或MKMapView)在大陆的数据源是高德的,采用GCJ-02标准。

  在app开发过程中,当坐标采用的标准与地图SDK的标准不一致时,需先按上表进行转换,然后使用SDK方法才能正确显示。
注意:在地图SDK上显示,经纬度必须采用SDK一致的标准。

例如:获取了酒店的coordinate显示到MKMapView上时,必须知道该coordinate采用的标准,转化为GCJ-02标准后,赋值给annotation后即可调用[self.mapView addAnnotation:annotation];方法)

采用如下转换方法:

CLLocationCoordinate2DnewCoor = [CoordConvertor bd09ToGcj02:originCoor];

如果需要调用百度地图app,直接传百度坐标标准的经纬度即可。其它地图也一样。


4.转换算法如下:

CoordConvertor.h

#import 
#import 

@interface CoordConvertor : NSObject

/**
 *  @brief  世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
 *
 *  ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
 *
 *  @param  location    世界标准地理坐标(WGS-84)
 *
 *  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location;

/**
 *  @brief  中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
 *
 *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
 *
 *  @param  location    中国国测局地理坐标(GCJ-02)
 *
 *  @return 世界标准地理坐标(WGS-84)
 */
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location;

/**
 *  @brief  世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
 *
 *  @param  location    世界标准地理坐标(WGS-84)
 *
 *  @return 百度地理坐标(BD-09)
 */
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location;

/**
 *  @brief  中国国测局地理坐标(GCJ-02)<火星坐标> 转换成 百度地理坐标(BD-09)
 *
 *  @param  location    中国国测局地理坐标(GCJ-02)<火星坐标>
 *
 *  @return 百度地理坐标(BD-09)
 */
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location;

/**
 *  @brief  百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
 *
 *  @param  location    百度地理坐标(BD-09)
 *
 *  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location;

/**
 *  @brief  百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
 *
 *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
 *
 *  @param  location    百度地理坐标(BD-09)
 *
 *  @return 世界标准地理坐标(WGS-84)
 */
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location;

@end


CoordConvertor.m


#import "CoordConvertor.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_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * 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_LON_MIN 72.004
#define RANGE_LAT_MAX 55.8271
#define RANGE_LAT_MIN 0.8293
// jzA = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
#define jzA 6378245.0
#define jzEE 0.00669342162296594323

@implementation CoordConvertor

+ (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 true;
    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 gcj02Encrypt: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


另外,javaScript中转化坐标和转换为地址的方法也一并记录如下:

function showPosition(position) {
     var ggPoint = new BMap.Point(position.coords.longitude, position.coords.latitude);
        // 根据坐标逆解析地址
     var geoc = new BMap.Geocoder();

//0:代表GPS,也可以是2:google坐标
        BMap.Convertor.translate(gpsPoint, 0, translateCallback);
    }
    //坐标转换完之后的回调函数
    translateCallback = function (data) {
        if (data.status === 0) {
            //  data.points[0];             //转换后的百度坐标(正确)
            baiduPoint = point;
            var geoc = new BMap.Geocoder();
            geoc.getLocation(baiduPoint, getCityByBaiduCoordinate);
        }
    };
    function getCityByBaiduCoordinate(rs) {
        baiduAddress = rs.addressComponents;
        var address = "百度标注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber;
    }

你可能感兴趣的:(iOS开发实战-地图篇)