views:

65

answers:

3

Hello,

Just a quick question on Core Location, I'm trying to calculate the distance between two points, code is below:

    -(void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation
    {   

    // Configure the new event with information from the location.
        CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
        CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];

        CLLocationDistance kilometers = [newCoordinate distanceFromLocation:oldCoordinate] / 1000; // Error ocurring here.
        CLLocationDistance meters = [newCoordinate distanceFromLocation:oldCoordinate]; // Error ocurring here.
}

I'm getting the following error on the last two lines:

error: cannot convert to a pointer type

I've been searching Google, but I cannot find anything.

EDIT: Removed second question and put it in a separate post.

Regards, Stephen

A: 

Try this instead:

CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation];

The method you're trying to use is a method on a CLLocation object :)

deanWombourne
A: 

The distance is calculated between 2 CLLocations and not between to coordinates.

You need to use these coordinates to get the CLLocations for the respective coordinates using the following line of code

CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];

Similarly for the other coordinate and then you can calculate the distance between these two locations using the following line of code

CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;

Hope this will help you.

Atulkumar V. Jain
A: 

The problem here is that you're calling an object method:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;

of class CLLocation.

CLLocationCoordinate2D is in fact a structure, consisting of two doubles:

typedef struct {
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
} CLLocationCoordinate2D;

Proper way of doing this is to get a CLLocation object and call distanceFromLocation on it. Like this:

    CLLocation* newLocation;
    CLLocation* oldLocation;
    CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];

Of course you first need to initialize both of those values (from CLLocationManager, for instance).

Pawel