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.
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)²).
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];
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.
Try this instead:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
The method you're trying to use is a method on a CLLocation object :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With