简体   繁体   中英

Need to know how to implement this code for Xcode Objective C

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

I found this code:

- (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);
}


Hello I am now to this iOS programing and I like to try out your code and see how it works out, now how do I implement this code? I can see the inputs of:

localCoordinate.latitude
localCoordinate.longitude
distance
bearing

the outputs of:

dLat2
dLon2

do I start the processes? are the inputs NSStrings?

azimuth = [NSString stringWithFormat:@"%@.%@", deg1, min1]; // Degrees and Minutes

intKiloMeters1 = [miles1 intValue] / 0.621371192;

NSLog(@"you get km  %d", intKiloMeters1);

int distance;

distance = intKiloMeters1;

double bearing = [self degreesToRadians:[azimuth doubleValue]];

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

double rLat1 = [self degreesToRadians:myCoordinate.latitude];     // Convert latitude to radians
double rLon1 = [self degreesToRadians:myCoordinate.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 finalLat1 = [self radiansToDegrees:rLat2];        // Convert latitude to degrees
double finalLong1 = [self radiansToDegrees:rLon2];        // Convert longuitude to degrees

NSLog(@"Export Lat %f Long %f", finalLat1, finalLong1);

CLLocationCoordinate2D unknownCoordinate = {finalLat1, finalLong1};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM