Moving a CLLocation by x meters

2019-01-08 22:46发布

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?

7条回答
干净又极端
2楼-- · 2019-01-08 23:24

Swift implementation using Measurement struct to do the conversions between degrees and radians.

class GPSLocation {

public class func degreesToRadians(degrees: Double) -> Double {
        return Measurement(value: degrees, unit: UnitAngle.degrees).converted(to: .radians).value
    }

    public class func radiansToDegrees(radians: Double) -> Double {
        return Measurement(value: radians, unit: UnitAngle.radians).converted(to: .degrees).value
    }

    public class func location(location: CLLocation, byMovingDistance distance: Double, withBearing bearingDegrees:CLLocationDirection) -> CLLocation {
        let distanceRadians: Double = distance / 6372797.6
        let bearingRadians: Double = GPSLocation.degreesToRadians(degrees: bearingDegrees)

        let lat1 = GPSLocation.degreesToRadians(degrees: location.coordinate.latitude)
        let lon1 = GPSLocation.degreesToRadians(degrees: location.coordinate.longitude)

        let lat2 = GPSLocation.radiansToDegrees(radians:asin(sin(lat1) * cos(distanceRadians) + cos(lat1) * sin(distanceRadians) * cos(bearingRadians)))
        let lon2 = GPSLocation.radiansToDegrees(radians:lon1 + atan2(sin(bearingRadians) * sin(distanceRadians * cos(lat1)), cos(distanceRadians) - sin(lat1) * sin(lat2)))

        return CLLocation(latitude: lat2, longitude: lon2)
    }

}
查看更多
可以哭但决不认输i
3楼-- · 2019-01-08 23:25

There is a C function that is close to what you are asking but it takes a bearing and distance. It's in my UtilitiesGeo class on github. You would pass the latitude and longitude from your CLLocation to it and then create a new CLLocation from the resulting lat2 and lon2 that it returns:

/*-------------------------------------------------------------------------
* Given a starting lat/lon point on earth, distance (in meters)
* and bearing, calculates destination coordinates lat2/lon2.
*
* all params in degrees
*-------------------------------------------------------------------------*/
void destCoordsInDegrees(double lat1, double lon1,
                         double distanceMeters, double bearing,
                         double* lat2, double* lon2);

If you can't use that, take a look at the algorithms that it was derived from here and here and perhaps you can modify it or those sites might have something closer to your needs.

查看更多
在下西门庆
4楼-- · 2019-01-08 23:30

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)
}
查看更多
三岁会撩人
5楼-- · 2019-01-08 23:34

Great post, here's the Obj-C wrapper for those who love copy/paste:

- (CLLocationCoordinate2D) locationWithBearing:(float)bearing distance:(float)distanceMeters fromLocation:(CLLocationCoordinate2D)origin {
    CLLocationCoordinate2D target;
    const double distRadians = distanceMeters / (6372797.6); // earth radius in meters

    float lat1 = origin.latitude * M_PI / 180;
    float lon1 = origin.longitude * M_PI / 180;

    float lat2 = asin( sin(lat1) * cos(distRadians) + cos(lat1) * sin(distRadians) * cos(bearing));
    float lon2 = lon1 + atan2( sin(bearing) * sin(distRadians) * cos(lat1),
                     cos(distRadians) - sin(lat1) * sin(lat2) );

    target.latitude = lat2 * 180 / M_PI;
    target.longitude = lon2 * 180 / M_PI; // no need to normalize a heading in degrees to be within -179.999999° to 180.00000°

    return target;
}
查看更多
Root(大扎)
6楼-- · 2019-01-08 23:35

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-08 23:38

A conversion to Swift, taken from this answer:

func locationWithBearing(bearing:Double, distanceMeters:Double, origin:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
    let distRadians = distanceMeters / (6372797.6) // earth radius in meters

    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(bearing))
    let lon2 = lon1 + atan2(sin(bearing) * sin(distRadians) * cos(lat1), cos(distRadians) - sin(lat1) * sin(lat2))

    return CLLocationCoordinate2D(latitude: lat2 * 180 / M_PI, longitude: lon2 * 180 / M_PI)
}

Morgan Chen wrote this:

All of the math in this method is done in radians. At the start of the method, lon1 and lat1 are converted to radians for this purpose as well. Bearing is in radians too. Keep in mind this method takes into account the curvature of the Earth, which you don't really need to do for small distances.

查看更多
登录 后发表回答