Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use iPhone method -(CLLocationDistance)distanceFromLocation:(const CLLocation *)location

I am a new iPhone developer learning Objective-C, and am trying to dynamically calculate the distances between the users latitude/longitude coordinates, with latitude/longitude coordinates in a SQLite table. I know that we can use CLLocations method:

(CLLocationDistance)distanceFromLocation:(const CLLocation *)location

to do this, but I'm not sure how to use it given the data that I have. How does one use the above method using pairs of latitude/longitude coordinates, considering the above method only deals with location objects of type CLLocation? Can anyone give me a simple example of how to use this method using two pairs of latitude/longitude coordinates?

like image 781
syedfa Avatar asked Feb 25 '23 21:02

syedfa


1 Answers

Just create CLLocation objects from your data:

// Assumption: lat1, lon1 and lat2, lon2 are double values containing the coordinates
CLLocation *firstLocation = [[[CLLocation alloc] initWithLatitude:lat1 longitude:lon1] autorelease];
CLLocation *secondLocation = [[[CLLocation alloc] initWithLatitude:lat2 longitude:lon2] autorelease];
CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];
like image 191
Ole Begemann Avatar answered Apr 30 '23 11:04

Ole Begemann