在實際開發過程中,由於各種地圖座標之間的偏差,混用導致結果不正確,但如果搞清楚他們採用何種座標,問題就迎刃而解了;
地球座標(WGS84)
- 國際標準
- 例如:CLLocationManager
火星座標 (GCJ-02)
- 中國標準
- 例如:iOS MKMapView、高德地圖、國內google 、搜搜、阿里雲
百度座標 (BD-09)
以下給出相應的轉換方法:
import UIKit
import CoreLocation
// --- transform_earth_from_mars ---
// 參考來源:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
// Krasovsky 1940
//
// 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
// --- transform_earth_from_mars end ---
// --- transform_mars_vs_bear_paw ---
// 參考來源:http://blog.woodbunny.com/post-68.html
let x_pi:Double = M_PI * 3000.0 / 180.0
public extension CLLocation {
/// 從地圖座標轉化到火星座標
///
/// - 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)
}
/// 從火星座標轉化到百度座標
///
/// - Returns: CLLocation
func locationBaiduFromMars() -> CLLocation {
let (n_lat,n_lng) = CLLocation.transform_mars_from_baidu(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
let coord_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)
let coord_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)
}
var dLat = CLLocation.transform_earth_from_mars_lat(lng - 105.0, lat - 35.0)
var dLng = CLLocation.transform_earth_from_mars_lng(lng - 105.0, lat - 35.0)
let radLat = lat / 180.0 * M_PI
var magic = sin(radLat)
magic = 1 - ee * magic * magic
let sqrtMagic: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 {
var ret: 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
return ret
}
private class func transform_earth_from_mars_lng(_ x: Double,_ y: Double) -> Double {
var ret: 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
return ret
}
private class func transform_mars_from_baidu(lat: Double, lng: Double) -> (Double,Double) {
let x = lng , y = lat
let z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi)
let theta = 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) {
let x = lng - 0.0065 , y = lat - 0.006
let z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi)
let theta = 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) {
return true
}
if (lat < 0.8293 || lat > 55.8271) {
return true
}
return false
}
}
結束