DistanceFromLocation - Calculate the distance between two points

Just a quick question about Core Location, I'm trying to calculate the distance between two points, the 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 get the following error in the last two lines:

error: cannot be converted to pointer type

I searched google but can't find anything.

+47
ios objective-c core-location
Oct 11 '10 at 11:50
source share
7 answers

Try this instead:

 CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; 

The method you are trying to use is the method of the CLLocation object :)

+110
Oct 11 2018-10-11
source share

The distance is calculated between 2 CLLocations, not between coordinates.

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

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

Similarly for a different 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 helps you.

Update: Swift 3.0

 let distanceKiloMeters = (newLocation.distance(from: oldLocation))/1000 
+22
Oct. 11 2018-10-11
source share

In swift

Create a method function that calculates the 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 need only two digits:

 extension Double{ var roundedTwoDigit:Double{ return Double(round(100*self)/100) } } 
+11
Jul 02 '15 at 4:28
source share

If you intend to use 2 values โ€‹โ€‹of CLLocationCoordinate2D, 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) } } 
+5
25 Oct '15 at 8:13
source share

The problem is that you are calling the object method :

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

class CLLocation.

CLLocationCoordinate2D is actually a two-locale structure:

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

The right way to do this is to get the CLLocation object and call distanceFromLocation on it. Like this:

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

Of course, first you need to initialize both of these values โ€‹โ€‹(for example, from CLLocationManager ).

+4
Oct 11 2018-10-11
source share
 #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 
+4
Sep 29 '16 at 5:09
source share

Adapted from the excellent CoreLocation Utility library :

 - (CLLocationDistance) distanceFromCoordinate:(CLLocationCoordinate2D) fromCoord; { double earthRadius = 6371.01; // Earth 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 } 
+2
Jan 22 '14 at 19:24
source share



All Articles