首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将CLLocation移动x米

将CLLocation移动x米
EN

Stack Overflow用户
提问于 2011-09-02 08:26:22
回答 9查看 13.2K关注 0票数 29

我定义了一个CLLocation,我想将该点向东移动x米,向南移动y米。我如何才能做到这一点?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2011-09-02 09:06:59

有一个C函数,它与你所要求的很接近,但它需要一个方向和距离。它在我的github上的UtilitiesGeo类中。您可以将纬度和经度从CLLocation传递给它,然后从它返回的结果lat2和lon2创建一个新的CLLocation:

代码语言:javascript
复制
/*-------------------------------------------------------------------------
* 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);

如果你不能使用它,看看它从herehere派生出来的算法,也许你可以修改它,或者这些网站可能有更接近你需要的东西。

票数 6
EN

Stack Overflow用户

发布于 2015-06-30 08:02:28

改进了Peters答案的快速解决方案。唯一的修正是轴承应该是弧度,而已经进行了计算。

代码语言:javascript
复制
 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)
}
票数 21
EN

Stack Overflow用户

发布于 2013-11-27 20:02:41

很棒的帖子,这里是为那些喜欢复制/粘贴的人准备的Obj-C包装器:

代码语言:javascript
复制
- (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;
}
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7278094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档