位置的距离总是 return 结果 0 - ios sdk

Distance for location always return result 0 - ios sdk

我已经计算了如下 2 个位置距离,结果总是 0 米。 有人可以帮忙吗?

double distance = [self.currentLocation distanceFromLocation:location1];
NSLog(@"Distance i meters: %.8f", distance);
if(distance <30)
{
    NSLog(@"Yeah, this place is inside my circle");
}
else
{
    NSLog(@"Oops!! its too far");
 }

谢谢

试试这个代码:使用 CLLocationDistance 获取距离:

 CLLocationDistance distance = [self.currentLocation distanceFromLocation:location1];
 float TotalMile=distance/1609.344;

我用下面的编码得到了正确的结果:

 CLLocation *location1 = [[CLLocation alloc] initWithLatitude:DEFAULT_LAT longitude:DEFAULT_LNG];

    CLLocationCoordinate2D annocoord = CLLocationCoordinate2DMake(location1.coordinate.latitude, location1.coordinate.longitude);
    CLLocationCoordinate2D usercoord = self.locationManager.location.coordinate;

   CLLocation *loc = [[CLLocation alloc] initWithLatitude:annocoord.latitude longitude:annocoord.longitude];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.locationManager.location.coordinate.latitude longitude:self.locationManager.location.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!