CLLocation distanceFromLocation

前端 未结 2 1496
悲哀的现实
悲哀的现实 2021-02-10 23:44

I am using CLLocation to work out the distance from the current user location, and an annotation. However I just wanted to know if this would be correct. I am currently using iP

相关标签:
2条回答
  • 2021-02-11 00:25

    You have two bad habits.

    1. You should not depend on simulator in situations need hardware censor status. Especially when you want correct test.
    2. You're handling types incorrectly. So you can't check values correctly. How is the longitude -1067024384? Longitude value is degrees. This means it's valid range is limited -90.0 ~ +90.0 by definition of longitude.

    Your longitude value is out of range. This means one of these. You printed the value wrongly or the real value was wrong. Simulator can print wrong value. Or you printed the value with wrong method. You have to try:

    Test on real device which has real hardware censors.

    If bad result continues after that,

    Review ALL of your application code. Especially for printing, handling values. Check you're using correct types and castings in > each situations. Because you may did buggy operation in somewhere habitually.

    And also, I recommend checking all of 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!
    
    0 讨论(0)
  • 2021-02-11 00:32

    Try @"%f" and don't cast it that way.

    In CLLocation.h

    typedef double CLLocationDistance;
    
    0 讨论(0)
提交回复
热议问题