Get the GPS coordinate given the current location,

2019-08-27 00:01发布

I'm trying to get a GPS coordinate given the current location, bearing and distance.

But I can't find any related to cocoa-touch.

I've found the formula here Get lat/long given current point, distance and bearing

Is there an easier method for iOS?

Thanks in advance.

1条回答
叛逆
2楼-- · 2019-08-27 00:13

Converted the code in the link to Objective-C.

- (double)degreesToRadians:(double)degrees {
    return degrees * M_PI / 180;
}

- (double)radiansToDegrees:(double)radians {
    return radians * 180/ M_PI;
}


- (CLLocationCoordinate2D)remoteCoordinate:(CLLocationCoordinate2D)localCoordinate withDistance:(double)distance withBearing:(double)bearing {

    double earthRadius = 6378.1;         // Radius of Earth in kilometres.

    double rLat1 = [self degreesToRadians:localCoordinate.latitude];     // Convert latitude to radians
    double rLon1 = [self degreesToRadians:localCoordinate.longitude];    // Convert longitude to radians

    double rLat2 = asinl( sinl(rLat1) * cosl(distance / earthRadius) + cosl(rLat1) * sinl(distance / earthRadius) * cosl(bearing) );
    double rLon2 = rLon1 + atan2l( sinl(bearing) * sinl(distance/earthRadius) * cosl(rLat1), cosl(distance/earthRadius) - sinl(rLat1) * sinl(rLat2) );

    double dLat2 = [self radiansToDegrees:rLat2];        // Convert latitude to degrees
    double dLon2 = [self radiansToDegrees:rLon2];        // Convert longuitude to degrees

    return CLLocationCoordinate2DMake(dLat2, dLon2);
}
查看更多
登录 后发表回答