位置距离始终返回结果0 - ios sdk

时间:2016-08-12 03:40:31

标签: ios ios9 cllocationmanager distance

我计算了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");
 }

由于

2 个答案:

答案 0 :(得分:0)

试用此代码:使用CLLocationDistance获取距离:

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

答案 1 :(得分:0)

我通过以下编码得到了正确的结果:

 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!