distanceFromLocation - Calculate distance between

2019-01-07 06:24发布

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.

7条回答
再贱就再见
2楼-- · 2019-01-07 06:46

Taken from the excellent libary CoreLocation utitlities :

- (CLLocationDistance) distanceFromCoordinate:(CLLocationCoordinate2D) fromCoord;
{
    double earthRadius = 6371.01; // Earth's radius in Kilometers

    // Get the difference between our two points then convert the difference into radians
    double nDLat = (fromCoord.latitude - self.coordinate.latitude) * kDegreesToRadians;  
    double nDLon = (fromCoord.longitude - self.coordinate.longitude) * kDegreesToRadians; 

    double fromLat =  self.coordinate.latitude * kDegreesToRadians;
    double toLat =  fromCoord.latitude * kDegreesToRadians;

    double nA = pow ( sin(nDLat/2), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2), 2 );

    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = earthRadius * nC;

    return nD * 1000; // Return our calculated distance in meters
}
查看更多
登录 后发表回答