Chris
Chris

Reputation: 489

Calculate location by distance from other location

I know there are tons of questions (and answers) on how to get the distance between two CLLocations. But I didn't find one single hint on how to do this the other way round.

Here's the concrete situation: I have one CLLocation, one distance (let's say 200 meters) and one direction. Now I need to know how to calculate the location that is 200 meters away (in a specified direction) from the first location. I don't know what could be the best format for the direction. Maybe the best would be in degrees (north = 0, east = 90, south = 180, west = 270).

In a nutshell, I need a method that could be defined like

-(CLLocation*)locationWithDistance:(int)meters inDirection:(int)degrees fromLocation:(CLLocation*)origin;

Thanks a lot for your help.

Upvotes: 2

Views: 783

Answers (2)

progrmr
progrmr

Reputation: 77281

There is C code already written to do exactly that. It is posted on github here. There is a degrees and radians version of the function in UtilitiesGeo.c/.h files. It could have been written with an Objective-C style but I wanted it to be portable to any C based project.

/*-------------------------------------------------------------------------
* 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);

Upvotes: 0

manujmv
manujmv

Reputation: 6445

For more accurate method use this formula in the link

http://www.movable-type.co.uk/scripts/latlong.html

Please find the section "Destination point given distance and bearing from start point". Apply it in your code

Upvotes: 1

Related Questions