不知道大家有没有遇到过这样的情况,在APP上开启定位,会发现自己所在的位置和地图上会有一定的偏差,前2年的时候这个问题特别严重,记得快的打车(那会儿还没有和滴滴合并)的坐标也会出现这种问题。我之所以记录这个是发现不少记录运动轨迹的应用(例如乐心),包括一大票水土不服的国外优秀运动APP不少还有这个问题。
为什么会出现这样的情况呢?
火星坐标
我们先来认识一个只有在我大天朝才会出现的词:火星坐标
什么叫做火星坐标?就是 不是地球上的真实坐标就对了。 废话。。。原因是我大天朝上面的所有东西都是国家机密!嘘,不要问我为什么知道。所以,我们的地图要经过偏移处理,你在经过我天朝测绘局处理的地图上看到的建筑物,和它实际所在的位置,其实是有出入的。
也不知道这群人搞出这么自欺欺人的东西目的何在,Google Earth上面看的清清楚楚的,不太明白有什么用处。。。反正就是给码农添堵就是了。
与之对应的,当然就是地球坐标了,就是除了在天朝之外的地球上,大家都统一使用的一套经纬度坐标。
举个栗子:
假如你开发了一个APP程序,那你获取当前设备的GPS信息,那获取到的这个坐标就是地球坐标,好了,如果你把这个坐标设置在经过我大天朝认可的移动设备地图上后,就会出现偏差。因为在经过偏移后的地图上面,只有把你获取到的坐标经过处理,换算成火星坐标,才能在地图上显示正确的位置。
那说到这里,我们都很明白了,如果自己开发一款程序需要自己获取经纬度坐标,然后在地图上打点,就需要经过 地球坐标---> 火星坐标 之间的转换,然后在地图上把,火星坐标打上去,才让用户觉得你的这个坐标是正确的。
地球坐标 –> 火星坐标
直接上代码:
GetMarsCoor.h
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | 
 
 
 
 
 
 
 #import <Foundation/Foundation.h>
 #import <MapKit/MapKit.h>
 
 
 @interface GetMarsCoor : NSObject
 + (CLLocation *)transformToMars:(CLLocation *)location;
 + (BOOL)outOfChina:(CLLocation *)location;
 + (double)transformLatWithX:(double)x y:(double)y;
 + (double)transformLonWithX:(double)x y:(double)y;
 @end
 
 | 
GetMarsCoor.m
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 
 | 
 
 
 
 
 
 #import "GetMarsCoor.h"
 
 @implementation GetMarsCoor
 
 + (CLLocation *)transformToMars:(CLLocation *)location {
 const double a = 6378245.0;
 const double ee = 0.00669342162296594323;
 
 if ([[self class] outOfChina:location]) {
 return location;
 }
 double dLat = [[self class] transformLatWithX:location.coordinate.longitude - 105.0 y:location.coordinate.latitude - 35.0];
 double dLon = [[self class] transformLonWithX:location.coordinate.longitude - 105.0 y:location.coordinate.latitude - 35.0];
 double radLat = location.coordinate.latitude / 180.0 * M_PI;
 double magic = sin(radLat);
 magic = 1 - ee * magic * magic;
 double sqrtMagic = sqrt(magic);
 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
 dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
 return [[CLLocation alloc] initWithLatitude:location.coordinate.latitude + dLat longitude:location.coordinate.longitude + dLon];
 }
 
 + (BOOL)outOfChina:(CLLocation *)location {
 if (location.coordinate.longitude < 72.004 || location.coordinate.longitude > 137.8347) {
 return YES;
 }
 if (location.coordinate.latitude < 0.8293 || location.coordinate.latitude > 55.8271) {
 return YES;
 }
 return NO;
 }
 
 + (double)transformLatWithX:(double)x y:(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 * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
 ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
 ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
 return ret;
 }
 
 + (double)transformLonWithX:(double)x y:(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 * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
 ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
 ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
 return ret;
 }
 
 @end
 
 | 
亲测这套代码在iOS上面是可以完美转换地球坐标到火星坐标。
因为年代久远还是用objective-c写的,不过在swift中使用也没有问题吧。
我刚想起来博客地址迁移,没有把在Swift中使用objective-c编写的库的方法迁移过来。回头把那篇文章迁移过来,然后再更新下链接吧~