Moving a CLLocation by x meters

前端 未结 11 958
余生分开走
余生分开走 2020-11-30 04:54

I have a CLLocation defined, and I\'d like to move that point x meters to the east and y meters to the south. How may I achieve that?

11条回答
  •  失恋的感觉
    2020-11-30 05:33

    Improved swift solution to Peters answer. Only correction is the bearing should be radian while calculation has been made.

     func locationWithBearing(bearing:Double, distanceMeters:Double, origin:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        let distRadians = distanceMeters / (6372797.6)
    
        var rbearing = bearing * M_PI / 180.0
    
        let lat1 = origin.latitude * M_PI / 180
        let lon1 = origin.longitude * M_PI / 180
    
        let lat2 = asin(sin(lat1) * cos(distRadians) + cos(lat1) * sin(distRadians) * cos(rbearing))
        let lon2 = lon1 + atan2(sin(rbearing) * sin(distRadians) * cos(lat1), cos(distRadians) - sin(lat1) * sin(lat2))
    
        return CLLocationCoordinate2D(latitude: lat2 * 180 / M_PI, longitude: lon2 * 180 / M_PI)
    }
    

提交回复
热议问题