distanceFromLocation - Calculate distance between two points

IosObjective CCore Location

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

Try this instead:

CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

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

Solution 2 - Ios

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.

Update: Swift 3.0

let distanceKiloMeters = (newLocation.distance(from: oldLocation))/1000

Solution 3 - Ios

In Swift

Let's create a method function that calculates distance between two locations:

 func distanceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
        
        var distanceMeters = source.distanceFromLocation(destination)
        var distanceKM = distanceMeters / 1000
        let roundedTwoDigit = distanceKM.roundedTwoDigit
        return roundedTwoDigit
        
    }

If you want only two digits:

extension Double{
    
    var roundedTwoDigit:Double{
        
        return Double(round(100*self)/100)

        }
    }

Solution 4 - Ios

If you're going to do with 2 CLLocationCoordinate2D values then you can use this.

This is Swift 2.1 on Xcode 7.1

import CoreLocation

extension CLLocationCoordinate2D {

    func distanceInMetersFrom(otherCoord : CLLocationCoordinate2D) -> CLLocationDistance {
        let firstLoc = CLLocation(latitude: self.latitude, longitude: self.longitude)
        let secondLoc = CLLocation(latitude: otherCoord.latitude, longitude: otherCoord.longitude)
        return firstLoc.distanceFromLocation(secondLoc)
    }

}

Solution 5 - Ios

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).

Solution 6 - Ios

#import <CoreLocation/CoreLocation.h>

CLLocation *locA = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];
                    
CLLocation *locB = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];
                    
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"distance:-%f",distance);//distance in Miter

Solution 7 - Ios

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephenView Question on Stackoverflow
Solution 1 - IosdeanWombourneView Answer on Stackoverflow
Solution 2 - IosAtulkumar V. JainView Answer on Stackoverflow
Solution 3 - Iosuser4790024View Answer on Stackoverflow
Solution 4 - IosMax AlexanderView Answer on Stackoverflow
Solution 5 - IosPawelView Answer on Stackoverflow
Solution 6 - IosYogesh TarsariyaView Answer on Stackoverflow
Solution 7 - IoszerodogView Answer on Stackoverflow