[轉(zhuǎn)]iOS地球坐標、火星坐標和百度坐標之間轉(zhuǎn)換(Swift3.0)

在實際開發(fā)過程中,由于各種地圖坐標之間的偏差,混用導(dǎo)致結(jié)果不正確,但如果搞清楚他們采用何種坐標,問題就迎刃而解了;

地球坐標(WGS84)

國際標準

例如:CLLocationManager

火星坐標 (GCJ-02)

中國標準

例如:iOS MKMapView、高德地圖、國內(nèi)google 、搜搜、阿里云

百度坐標 (BD-09)

百度標準

例如:百度 SDK,地圖

以下給出相應(yīng)的轉(zhuǎn)換方法:

// a = 6378245.0, 1/f = 298.3

// b = a * (1 - f)

// ee = (a^2 - b^2) / a^2;

let a:Double = 6378245.0

let ee:Double = 0.00669342162296594323

letx_pi:Double=M_PI*3000.0/180.0

importCoreLocation

public extension CLLocation {

? ? /// 從地圖坐標轉(zhuǎn)化到火星坐標

? ? ///

? ? /// - Returns: CLLocation

? ? func locationMarsFromEarth() -> CLLocation {

? ? ? ? let (n_lat,n_lng) = CLLocation.transform_earth_from_mars(lat: self.coordinate.latitude, lng: self.coordinate.longitude)

? ? ? ? let coord_2d = CLLocationCoordinate2D(latitude: n_lat + self.coordinate.latitude, longitude: n_lng + self.coordinate.longitude)


? ? ? ? return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp)

? ? }


? ? /// 從火星坐標到地圖坐標

? ? ///

? ? /// - Returns: CLLocation

? ? func locationEarthFromMars() -> CLLocation {


? ? ? ? let (n_lat,n_lng) = CLLocation.transform_earth_from_mars(lat: self.coordinate.latitude, lng: self.coordinate.longitude)

? ? ? ? let coord_2d = CLLocationCoordinate2D(latitude: self.coordinate.latitude - n_lat, longitude: self.coordinate.longitude - n_lng)


? ? ? ? return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp)

? ? }


? ? /// 從火星坐標轉(zhuǎn)化到百度坐標

? ? ///

? ? /// - Returns: CLLocation

? ? func locationBaiduFromMars() -> CLLocation {


? ? ? ? let (n_lat,n_lng) = CLLocation.transform_mars_from_baidu(lat: self.coordinate.latitude, lng: self.coordinate.longitude)

? ? ? ? letcoord_2d =CLLocationCoordinate2D(latitude: n_lat,longitude: n_lng)


? ? ? ? return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp)

? ? }


? ? /// 從百度坐標到火星坐標

? ? ///

? ? /// - Returns: CLLocation

? ? func locationMarsFromBaidu() -> CLLocation {

? ? ? ? let (n_lat,n_lng) = CLLocation.transform_baidu_from_mars(lat: self.coordinate.latitude, lng: self.coordinate.longitude)

? ? ? ? letcoord_2d =CLLocationCoordinate2D(latitude: n_lat,longitude: n_lng)


? ? ? ? return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp)

? ? }


? ? /// 從百度坐標到地圖坐標

? ? ///

? ? /// - Returns: CLLocation

? ? func locationEarthFromBaidu() -> CLLocation {


? ? ? ? let mars = self.locationMarsFromBaidu()

? ? ? ? let (n_lat,n_lng) = CLLocation.transform_earth_from_mars(lat: mars.coordinate.latitude, lng: mars.coordinate.longitude)

? ? ? ? let coord_2d = CLLocationCoordinate2D(latitude: self.coordinate.latitude - n_lat, longitude: self.coordinate.longitude - n_lng)


? ? ? ? return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp)

? ? }


? ? private class func transform_earth_from_mars(lat: Double, lng: Double) -> (Double, Double) {

? ? ? ? if CLLocation.transform_sino_out_china(lat: lat, lng: lng) {

? ? ? ? ? ? return(lat, lng)

? ? ? ? }

? ? ? ? vardLat =CLLocation.transform_earth_from_mars_lat(lng -105.0, lat -35.0)

? ? ? ? vardLng =CLLocation.transform_earth_from_mars_lng(lng -105.0, lat -35.0)

? ? ? ? letradLat = lat /180.0*M_PI

? ? ? ? varmagic =sin(radLat)

? ? ? ? magic =1-ee* magic * magic

? ? ? ? letsqrtMagic:Double=sqrt(magic)

? ? ? ? dLat = (dLat *180.0) / ((a* (1-ee)) / (magic * sqrtMagic) *M_PI)

? ? ? ? dLng = (dLng *180.0) / (a/ sqrtMagic *cos(radLat) *M_PI)

? ? ? ? return(dLat,dLng)

? ? }

? ? private class func transform_earth_from_mars_lat(_ x: Double,_ y: Double) -> Double {

? ? ? ? varret:Double= -100.0+2.0* x +3.0* y +0.2* y * y +0.1* x * y +0.2*sqrt(fabs(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

? ? ? ? returnret

? ? }

? ? private class func transform_earth_from_mars_lng(_ x: Double,_ y: Double) -> Double {

? ? ? ? varret:Double=300.0+ x +2.0* y +0.1* x * x +0.1* x * y +0.1*sqrt(fabs(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

? ? ? ? returnret

? ? }

? ? private class func transform_mars_from_baidu(lat: Double, lng: Double) -> (Double,Double) {

? ? ? ? letx = lng , y = lat

? ? ? ? letz =sqrt(x * x + y * y) +0.00002*sin(y *x_pi)

? ? ? ? lettheta =atan2(y, x) +0.000003*cos(x *x_pi)

? ? ? ? return(z *sin(theta) +0.006, z *cos(theta) +0.0065)

? ? }

? ? private class func transform_baidu_from_mars(lat: Double, lng: Double) -> (Double,Double) {

? ? ? ? letx = lng -0.0065, y = lat -0.006

? ? ? ? letz =? sqrt(x * x + y * y) -0.00002*sin(y *x_pi)

? ? ? ? lettheta =atan2(y, x) -0.000003*cos(x *x_pi)

? ? ? ? return(z *sin(theta), z *cos(theta))

? ? }

? ? private class func transform_sino_out_china(lat: Double, lng: Double) -> Bool {

? ? ? ? if(lng <72.004|| lng >137.8347) {

? ? ? ? ? ? returntrue

? ? ? ? }

? ? ? ? if(lat <0.8293|| lat >55.8271) {

? ? ? ? ? ? returntrue

? ? ? ? }

? ? ? ? return false

? ? }

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容