One of the most common task while using the GPS device of your iPhone is the computation of the distance between two locations, normally the location of the iPhone/iPad and the location of a point of interest (POI).

The CLLocation class provides a method which helps a lot, it is the distanceFromLocation method; it is very simple to use take a look at the following code:

double distance = [iphoneLocation distanceFromLocation:poiLocation];

 

Where iphoneLocation and poiLocatione are both CLLocation's, distance is the distance between iphoneLocation and poiLocation.

gps-const

 

The problems is that often (if we don't want to say "always") the POI location is expressed in latitude and longitude coordinates (poiLocation.lat and poiLocation.long), so we have to transform the latitude and longitude coordinates in a CLLocation, it's really simple:

CLLocation *l2 = [[CLLocation alloc] initWithLatitude:poiLocation.lat longitude:poiLocation.long];

Soon I'm going to publish a complete tutorial on Using the "GPS Device on the iPhone", keep in touch.

Gg1