内容简介:对于国内地图而言,使用LocationManager定位所获得经纬度,是有一段较大距离的偏移的。原因是国内地图使用的坐标系统是GCJ-02而iossdk中所用到的是国际标准的坐标系统WGS-84。 因为国内使用的是加密后的坐标系GCJ-02就是网络上叫的火星坐标。 locationManager就是因为得到的是火星坐标偏移后的经纬度,所以导致在MapView上有很大的偏差,而在MKMapView上通过定位自己位置所获得的经纬度有是准确,因为apple已经对国内地图做了偏移优化。Github上有人写了一个现成
-
(地球坐标)WGS84
- 国际标准,GPS标准从GPS设备中取出的原始数据是就是这个
- 国际地图提供商一般使用的也是这个
-
火星坐标(GCJ-02)
国内出版的各种地图系统(包括电子形式),必须至少采用GCJ-02对地理位置进行首次加密。
-
百度坐标(BD-09)
- 百度标准,百度SDK,地图,Geocoding用的都是这个。
对于国内地图而言,使用LocationManager定位所获得经纬度,是有一段较大距离的偏移的。原因是国内地图使用的坐标系统是GCJ-02而iossdk中所用到的是国际标准的坐标系统WGS-84。 因为国内使用的是加密后的坐标系GCJ-02就是网络上叫的火星坐标。 locationManager就是因为得到的是火星坐标偏移后的经纬度,所以导致在MapView上有很大的偏差,而在MKMapView上通过定位自己位置所获得的经纬度有是准确,因为apple已经对国内地图做了偏移优化。
Github上有人写了一个现成的转换类,可以参考参考 传送门 ,其主要代码见下:
- .h文件
#import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface JZLocationConverter : 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 复制代码
- .m文件
#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_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 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 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
复制代码
- 使用示例
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
// 获取最新CLLocation
CLLocation *lastLocation = [locations objectAtIndex:0];
// 转化为火星坐标
CLLocationCoordinate2D coord = [lastLocation coordinate];
coord = [BXJZLocationConverter wgs84ToGcj02:coord];
}
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- arcgis for jsapi开发:坐标系、经纬度与平面坐标的互换
- iOS坐标系探究
- canvas-坐标系、圆角矩形、纹理、剪裁
- android自定义View——坐标系
- OpenGL ES 高级进阶:坐标系及矩阵变换
- View 体系详解:坐标系、滑动、手势和事件分发机制
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Intersectional Internet
Safiya Umoja Noble、Brendesha M. Tynes / Peter Lang Publishing / 2016
From race, sex, class, and culture, the multidisciplinary field of Internet studies needs theoretical and methodological approaches that allow us to question the organization of social relations that ......一起来看看 《The Intersectional Internet》 这本书的介绍吧!