我定义了一个CLLocation,我想将该点向东移动x米,向南移动y米。我如何才能做到这一点?
发布于 2011-09-02 09:06:59
有一个C函数,它与你所要求的很接近,但它需要一个方向和距离。它在我的github上的UtilitiesGeo类中。您可以将纬度和经度从CLLocation传递给它,然后从它返回的结果lat2和lon2创建一个新的CLLocation:
/*-------------------------------------------------------------------------
* 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);
发布于 2015-06-30 08:02:28
改进了Peters答案的快速解决方案。唯一的修正是轴承应该是弧度,而已经进行了计算。
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)
}
发布于 2013-11-27 20:02:41
很棒的帖子,这里是为那些喜欢复制/粘贴的人准备的Obj-C包装器:
- (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;
}
https://stackoverflow.com/questions/7278094
复制相似问题