CLLocation distanceFromLocation

I use CLLocation to determine the distance from the user's current location and annotations. However, I just wanted to know if that would be right. I am currently using iPhone Simulator for this, and according to MKMapView iPhone Simulator is here:

Lat: 0 Long: -1067024384 

Annotation Position:

 workingCoordinate.latitude = 40.763856; workingCoordinate.longitude = -73.973034; 

However, if you look at Google maps, you will find out how close these distances are, but so far isolated according to CLLocation. I use the following code to determine the distance between them.

 CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude]; CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude]; CLLocationDistance dist = [loc distanceFromLocation:loc2]; int distance = dist NSLog(@"%i", distance); 

The NSregged distance is 12769908. I believe this is incorrect, and therefore there should be a problem with my code.

If there is, please indicate this!

+4
source share
2 answers

You have two bad habits.

  • You should not depend on the simulator in situations requiring the status of a hardware censor. Especially if you need the right test.
  • You are not handling types correctly. Therefore, you cannot check the values ​​correctly. How -1067024384 ? The value of longitude is degrees. This means that the valid range is limited to -90.0 ~ +90.0 by definition of longitude.

Your longitude value is out of range. This means one of them. You printed the value incorrectly or the actual value was incorrect. The simulator may print the wrong value. Or you printed the value with the wrong method. You must try:

Test on a real device with real hardware censors.

If after that a bad result occurs,

View ALL of your application code. Especially for printing, processing values. Make sure you use the right types and castings in every situation. Because you can work with the error somewhere usually.

In addition, I recommend checking all intermediate values ​​like this.

 CLLocationCoordinate2D annocoord = annotation.coordinate; CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate; NSLog(@"ANNO = %f, %f", annocoord.latitude, annocoord.longitude); NSLog(@"USER = %f, %f", usercoord.latitude, usercoord.longitude); CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude]; CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude]; NSLog(@"LOC = %f, %f", loc.coordinate.latitude, loc.coordinate.longitude); NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude); CLLocationDistance dist = [loc distanceFromLocation:loc2]; NSLog(@"DIST: %f", dist); // Wrong formatting may show wrong value! 
+9
source

Try @ "% f" and don’t throw it this way.

In CLLocation.h

 typedef double CLLocationDistance; 
+1
source

Source: https://habr.com/ru/post/1342355/


All Articles