0

有没有人根据起点,方位和距离计算新的纬度和经度?

我将非常感谢人们可能获得的任何帮助。

4

2 回答 2

4

我使用了来自计算新坐标 x 米和 y 度距一个坐标的代码:

- (CLLocationCoordinate2D)coordinateFromCoord:(CLLocationCoordinate2D)fromCoord
                                 atDistanceKm:(double)distanceKm
                             atBearingDegrees:(double)bearingDegrees
{
    double distanceRadians = distanceKm / 6371.0;
    //6,371 = Earth's radius in km
    double bearingRadians = [self radiansFromDegrees:bearingDegrees];
    double fromLatRadians = [self radiansFromDegrees:fromCoord.latitude];
    double fromLonRadians = [self radiansFromDegrees:fromCoord.longitude];

    double toLatRadians = asin(sin(fromLatRadians) * cos(distanceRadians)
                               + cos(fromLatRadians) * sin(distanceRadians) * cos(bearingRadians) );

    double toLonRadians = fromLonRadians + atan2(sin(bearingRadians)
                                                 * sin(distanceRadians) * cos(fromLatRadians), cos(distanceRadians)
                                                 - sin(fromLatRadians) * sin(toLatRadians));

    // adjust toLonRadians to be in the range -180 to +180...
    toLonRadians = fmod((toLonRadians + 3*M_PI), (2*M_PI)) - M_PI;

    CLLocationCoordinate2D result;
    result.latitude = [self degreesFromRadians:toLatRadians];
    result.longitude = [self degreesFromRadians:toLonRadians];

    return result;
}

- (double)radiansFromDegrees:(double)degrees
{
    return degrees * (M_PI/180.0);
}

- (double)degreesFromRadians:(double)radians
{
    return radians * (180.0/M_PI);
}

或者在 Swift 中:

extension CLLocationCoordinate2D {
    func adjusted(distance: Double, degrees: Double) -> CLLocationCoordinate2D {
        let distanceRadians = distance / 6_371  // 6,371 == Earth's radius in km
        let bearingRadians = degrees.radians
        let fromLatRadians = latitude.radians
        let fromLonRadians = longitude.radians

        let toLatRadians = asin(sin(fromLatRadians) * cos(distanceRadians) + cos(fromLatRadians) * sin(distanceRadians) * cos(bearingRadians))

        var toLonRadians = fromLonRadians + atan2(sin(bearingRadians)
                                                     * sin(distanceRadians) * cos(fromLatRadians), cos(distanceRadians)
                                                     - sin(fromLatRadians) * sin(toLatRadians))

        // adjust toLonRadians to be in the range -180 to +180...
        toLonRadians = fmod((toLonRadians + 3 * .pi), (2 * .pi)) - .pi

        return CLLocationCoordinate2D(latitude: toLatRadians.degrees, longitude: toLonRadians.degrees)
    }
}

extension CLLocationDegrees {
    var radians: Double { self * .pi / 180 }
}

extension Double {
    var degrees: CLLocationDegrees { self * 180 / .pi }
}
于 2013-05-31T05:37:48.850 回答
0

您可以在http://www.movable-type.co.uk/scripts/latlong.html找到您可能想要的所有计算(包括解释等)

您需要的代码(在 JavaScript 中)位于“目标点给定距离和距起点的方位”标题下。摘录:

var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
          Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                 Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

其中 R = 地球半径,d = 距离(以相同的单位),而纬度/经度以弧度为单位(因为这是sin函数所期望的)。你从度数到弧度数

radians = pi * degrees / 180;

你应该可以从这里拿走它。请查看我提供的链接以获取更多信息。

于 2013-05-31T05:03:34.647 回答