Moving a CLLocation by x meters

前端 未结 11 989
余生分开走
余生分开走 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 05:14

    A simpler solution is to use MKMapPoints.

    Convert your original coordinates, and any offset distances you need to MKMapPoints using this:

    let coordinatesInMapPoints = MKMapPointForCoordinate(CLLocationCoordinate2D)
    let distancesInMapPoints = yourDistanceInMeters * MKMapPointsPerMeterAtLatitude(CLLocationDegrees) // Do this for both x and y directions if needed.
    

    Then make a new MKMapPoint by simply adding your offset distances to your original coordinates:

    let newCoordinatesInMapPoints = MKMapPointMake(coordinatesInMapPoints.x + distancesInMapPoints, coordinatesInMapPoints.y)
    

    Finally, convert the new coordinates from a MKMapPoint back to CLLocationCoordinate2D:

    let newCoordinate = MKCoordinateForMapPoint(newCoordinatesInMapPoints)
    

    No complex conversion calculations needed.

提交回复
热议问题