转载:
文章一 http://blog.csdn.net/xiaobaismiley/article/details/37576303
文章二:http://blog.csdn.net/swingpyzf/article/details/16972351
文章一:
标准地球坐标即GPS设备获得的坐标,该坐标需要经过国家测绘局进行加密后形成火星坐标GCJ-02,国外坐标(WGS-84 )我们用的google坐标和高德地图坐标也就是火星坐标
百度地图,在火星坐标的基础上再进行一次加密,形成了百度地图上的坐标,因此,直接将标准地球坐标显示在百度地图上是会有几百米的偏差的。按照此原理,标准GPS坐标经过两步的转换可得到百度坐标。因为在处理百度地图时,例如查询其POI都需要百度地图上的坐标,而不是标准坐标,那么这样的转换就是必要的了。下面是两步转换的C++ 程序,供开发的人做一个参考。
- //============================================================================
- // Name : Convert_cpp.cpp
- // Author : roger
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- const double pi = 3.14159265358979324;
- const double a = 6378245.0;
- const double ee = 0.00669342162296594323;
- const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- bool outOfChina(double lat, double lon)
- {
- if (lon < 72.004 || lon > 137.8347)
- return true;
- if (lat < 0.8293 || lat > 55.8271)
- return true;
- return false;
- }
- double transformLat(double x, double y)
- {
- double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
- ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
- ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
- ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
- return ret;
- }
- double transformLon(double x, double y)
- {
- double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
- ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
- ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
- ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
- return ret;
- }
- /**
- * 地球坐标转换为火星坐标
- * World Geodetic System ==> Mars Geodetic System
- *
- * @param wgLat 地球坐标
- * @param wgLon
- *
- * mglat,mglon 火星坐标
- */
- void transform2Mars(double wgLat, double wgLon,double &mgLat,double &mgLon)
- {
- if (outOfChina(wgLat, wgLon))
- {
- mgLat = wgLat;
- mgLon = wgLon;
- return ;
- }
- double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
- double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
- double radLat = wgLat / 180.0 * pi;
- double magic = sin(radLat);
- magic = 1 - ee * magic * magic;
- double sqrtMagic = sqrt(magic);
- dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
- dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
- mgLat = wgLat + dLat;
- mgLon = wgLon + dLon;
- }
- /**
- * 火星坐标转换为百度坐标
- * @param gg_lat
- * @param gg_lon
- */
- void bd_encrypt(double gg_lat, double gg_lon,double &bd_lat,double & bd_lon)
- {
- double x = gg_lon, y = gg_lat;
- double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
- double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
- bd_lon = z * cos(theta) + 0.0065;
- bd_lat = z * sin(theta) + 0.006;
- }
- /**
- * 百度转火星
- * @param bd_lat
- * @param bd_lon
- */
- void bd_decrypt(double bd_lat, double bd_lon,double &gg_lat,double &gg_lon)
- {
- double x = bd_lon - 0.0065, y = bd_lat - 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);
- gg_lon = z * cos(theta);
- gg_lat = z * sin(theta);
- }
- int main() {
- double lat = 30.227607;
- double lon = 120.036565;
- //真实的经纬度转化为百度地图上的经纬度,便于计算百度POI
- double marsLat = 0;
- double marsLon = 0;
- double resultLat = 0;
- double resultLon = 0;
- transform2Mars(lat,lon,marsLat,marsLon);
- bd_encrypt(marsLat,marsLon,resultLat,resultLon);
- //30.2193456 120.0348264
- cout<<setprecision(10)<<resultLat<<" "<<setprecision(10)<<resultLon<<endl;
- }
-----------------------------------------------------------------------------------------------------
- GCJ-02坐标系统(火星坐标)简介:http://blog.csdn.net/giswens/article/details/8775121(存档:http://mapbd.com/cms/2012/07/25/)
- GCJ-02到真实坐标反向变换的理论基础:http://blog.csdn.net/giswens/article/details/8775213
- WGS84坐标与Web墨卡托坐标互转:http://blog.csdn.net/giswens/article/details/9634261
- 地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法:
- C#代码:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936(注解:http://blog.csdn.net/giswens/article/details/8775283)
- Java代码:http://emq.googlecode.com/svn/emq/src/Algorithm/Coords/Converter.java
- iOS代码:http://blog.csdn.net/giswens/article/details/8775183(存档:http://www.keakon.net/2011/07/02/WGS84坐标转火星坐标(iOS篇))
- 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法:http://blog.csdn.net/coolypf/article/details/8569813(解释:国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。百度坐标在此基础上,进行了BD-09二次加密措施,更加保护了个人隐私。百度对外接口的坐标系并不是GPS采集的真实经纬度,需要通过坐标转换接口进行转换。)
- 一种根据纠偏数据对火星坐标进行完美拟合的方法:http://blog.sina.com.cn/s/blog_538036cf0100pxbl.html
- 国内各地图API坐标系统比较:http://rovertang.com/archives/547
- 关于百度地图坐标转换接口的研究:http://rovertang.com/archives/24699
查询过资料可得,通过程序进行 标准坐标转火星坐标(google、高德),火星坐标转百度坐标,百度坐标转火星坐标,百度坐标转标准坐标都是可行的,火星直接转标准坐标转不了。
CLLocationManager类可以实时的获得我们位置的经纬度,并且可以通过经纬度在MapView上定位:
- //创建CLLocationManager对象
- CLLocationManager *locationManager = [[CLLocationManager alloc] init];
- //设置委托对象为自己
- [locationManager setDelegate:self];
- //要求CLLocationManager对象返回全部结果
- [locationManager setDistanceFilter:kCLDistanceFilterNone];
- //要求CLLocationManager对象的返回结果尽可能的精准
- [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
- //要求CLLocationManager对象开始工作,定位设备位置
- [locationManager startUpdatingLocation];
通过下面的CLLocationManager的委托方法可以得到或者更新locationManager的经纬度,并且显示到MapView上
- //CLLocationManager委托方法
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
- //得到newLocation
- CLLocation *loc = [locations objectAtIndex:0];
- }
但是对于国内地图而言,使用LocationManager定位所获得经纬度,是有一段较大距离的 偏移 的。
这是为什么呢??这几天一直在查这方面的资料,各种google、baidu论坛里给出的答案也各有不同:
wifi 热点 基站 gps等等所导致的都有,最后查找还是找到了问题所在,原来国内地图使用的坐标系统是 GCJ-02 而ios sdk中所用到的是国际标准的坐标系统 WGS-84 。
因为国内使用的是加密后的坐标系GCJ-02就是网络上叫的火星坐标。
locationManager就是因为得到的是火星坐标偏移后的经纬度,所以导致在MapView上有很大的偏差,而在MKMapView上通过定位自己位置所获得的经纬度有是准确,因为apple已经对国内地图做了偏移优化。
1、那么临时的解决方法:想要获得自己准确的经纬度可以直接通过MKMapView中对自身定位来获得:
- //定义一个MKMapView 并且调用setShowUserLocation:YES来获得自身的位置
- [self.mapView setShowsUserLocation:YES];
- //如果不想要显示这个MKMapView就将其隐藏
- [self.mapView setHidden:YES];
然后通过MKMapView的委托方法来获取准确的经纬度:
- -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
- CLLocationCoordinate2D coord = [userLocation coordinate];
- NSLog(@"经度:%f,纬度:%f",coord.latitude,coord.longitude);
- }
这个方法虽然简单,也可以实时的获取自己的位置而且基本上都是准确的位置,但是也就无法使用locationManager中的一些功能了。
那么有没有更好的解方法可以让我们在火星坐标上使用locationManager来获得准确的位置呢? 看了几篇博客给出的答案是相似的,就是调用apple的私有模块类中得方法来对经纬度做一个偏移修正:
http://blog.csdn.net/zhibudefeng/article/details/8495837
http://yach.me/blog/2011/11/09/huo-xing-zuo-biao-xi-wen-ti-zai-iosxia-de-xiu/
上面两篇文章中都提到了使用apple的私有模块MKLocationManager来修正偏移,不过遗憾的是如果使用了私有模块会导致AppStore审核失败,但是幸运的是这种方式只能在IOS5以前的系统中使用。。。。。。至少今天我们不用担心该不该使用这个方式。
接下来就是正题了:最后我找到了一份android的火星坐标转换的算法,然后自己改写成了以下将 WGS-84坐标系统转为GCJ-02 的Objective-C代码,这样就能方便的使用CLLocationManager这个类了。
2、将WGS-84转为GCJ-02(火星坐标):
新建一个类“WGS84TOGCJ02”,在.h头文件中定义:
- // Copyright (c) 2013年 swinglife. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <MapKit/MapKit.h>
- @interface WGS84TOGCJ02 : NSObject
- //判断是否已经超出中国范围
- +(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location;
- //转GCJ-02
- +(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc;
- @end
- // Copyright (c) 2013年 swinglife. All rights reserved.
- //
- #import "WGS84TOGCJ02.h"
- const double a = 6378245.0;
- const double ee = 0.00669342162296594323;
- const double pi = 3.14159265358979324;
- @implementation WGS84TOGCJ02
- +(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
- {
- CLLocationCoordinate2D adjustLoc;
- if([self isLocationOutOfChina:wgsLoc]){
- adjustLoc = wgsLoc;
- }else{
- double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
- double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
- double radLat = wgsLoc.latitude / 180.0 * pi;
- double magic = sin(radLat);
- magic = 1 - ee * magic * magic;
- double sqrtMagic = sqrt(magic);
- adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
- adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
- adjustLoc.latitude = wgsLoc.latitude + adjustLat;
- adjustLoc.longitude = wgsLoc.longitude + adjustLon;
- }
- return adjustLoc;
- }
- //判断是不是在中国
- +(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location
- {
- if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271)
- return YES;
- return NO;
- }
- +(double)transformLatWithX:(double)x withY:(double)y
- {
- double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
- lat += (20.0 * sin(6.0 * x * pi) + 20.0 *sin(2.0 * x * pi)) * 2.0 / 3.0;
- lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
- lat += (160.0 * sin(y / 12.0 * pi) + 3320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
- return lat;
- }
- +(double)transformLonWithX:(double)x withY:(double)y
- {
- double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
- lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
- lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
- lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
- return lon;
- }
- @end
最后我们通过判断isLocationOutOfChina 然后调用transformLatWithX方法就能获取转换后的 CLLocationCoordinate2D结构类型
- //CLLocationManager委托方法
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
- //得到newLocation
- CLLocation *loc = [locations objectAtIndex:0];
- //判断是不是属于国内范围
- if (![WGS84TOGCJ02 isLocationOutOfChina:[loc coordinate]]) {
- //转换后的coord
- CLLocationCoordinate2D coord = [WGS84TOGCJ02 transformFromWGSToGCJ:[loc coordinate]];
- }