[转]iOS地球坐标、火星坐标和百度坐标之间转换(Swift3.0)

在实际开发过程中,由于各种地图坐标之间的偏差,混用导致结果不正确,但如果搞清楚他们采用何种坐标,问题就迎刃而解了;

地球坐标(WGS84)

国际标准

例如:CLLocationManager

火星坐标 (GCJ-02)

中国标准

例如:iOS MKMapView、高德地图、国内google 、搜搜、阿里云

百度坐标 (BD-09)

百度标准

例如:百度 SDK,地图

以下给出相应的转换方法:

// 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 {

    /// 从地图坐标转化到火星坐标

    ///

    /// - 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)

        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

    }

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容