Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distanceFromLocation - Calculate distance between two points

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.

like image 287
Stephen Avatar asked Oct 11 '10 11:10

Stephen


People also ask

What is the formula for distance between two points?

What is Distance Between Two Points Formula? Distance between two points is the length of the line segment that connects the two points in a plane. The formula to find the distance between the two points is usually given by d=√((x2 – x1)² + (y2 – y1)²).

How do I find the distance between two lat long Swift?

You can use distanceFromLocation to find the distance between two coordinates. Code Snippets: CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1]; CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2]; CLLocationDistance distance = [loc1 distanceFromLocation:loc2];

How do you find the distance between two Geohashes?

The distance between two adjacent geohashes depends on the latitude & longitude of the center points. In order to get an accurate distance of your bounding box you need to get the adjacent boxes, calculate their center lat/lon and get the distance.


Video Answer


2 Answers

Try this instead:

CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; 

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

like image 190
deanWombourne Avatar answered Sep 22 '22 05:09

deanWombourne


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 
like image 29
Atulkumar V. Jain Avatar answered Sep 19 '22 05:09

Atulkumar V. Jain