比较CLLocationDistance的两个实例以找到较小的实例

时间:2011-05-23 01:17:30

标签: iphone cllocationmanager cllocationdistance

您好我的应用中有一系列mapAnnotations。每个注释对象都有一个地址和一个坐标。我正在使用我的位置管理器找出用户的当前位置,这是一个CLLocationDistance对象。然后我试图迭代mapAnnotations数组并将用户的位置与每个注释的位置坐标进行比较,以找到关闭用户的那个。 这是代码:

-(void)locationManager:(CLLocationManager*)_manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation
{
    self.userLocation = newLocation;
    NSLog(@"Usser location is: %@", self.userLocation);
    NSLog(@"USer location latitude = %.4f", self.userLocation.coordinate.latitude );
    NSLog(@"USer location longitude = %.4f", self.userLocation.coordinate.longitude);
    if (self.userLocation)
    {
        [self.locMgr stopUpdatingLocation];
    }
    [self calculateShortestDistanceBetweenUserAndBiz:self.userLocation];
}

//==============================================================================
-(void)calculateShortestDistanceBetweenUserAndBiz:(CLLocation *)_userLocation
{
    NSLog(@"Entering %s", __FUNCTION__);
    for(LocationMapAnnotation *tempAnnot in self.mapAnnotations)
    {
        CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:tempAnnot.coordinate.latitude longitude:tempAnnot.coordinate.longitude];
        NSLog(@"tempLocation created %@ ", tempLocation);
        CLLocationDistance tempDistance = [_userLocation distanceFromLocation: tempLocation];
        NSLog(@"tempDistance  was calculated at %d", tempDistance);
        if(!self.shortestDistance) 
        {
            self.shortestDistance = tempDistance;
            NSLog(@"Assigned value %d to shortestDistance", self.shortestDistance);
            continue;
        }
        if(self.shortestDistance >= tempDistance)
        {
        NSLog(@"Replacing shortest distance value from %d to %d", self.shortestDistance, tempDistance);
            self.shortestDistance = tempDistance;
        }
    }
}

该方法工作正常,这是控制台输出:

Created a new locationmodel

Usser location is: <+43.47878384, -80.53074371> +/- 1629.00m (speed -1.00 mps / course -1.00) @ 11-05-22 8:58:10 PM Eastern Daylight Time

USer location latitude = 43.4788

USer location longitude = -80.5307

Entering -[locationModel calculateShortestDistanceBetweenUserAndBiz:]

tempLocation created <+43.69445000, -79.75080000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 629486151

Assigned value 629486151 to shortestDistance

tempLocation created <+43.62192000, -79.52238000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 209234637
tempLocation created <+43.59233000, -79.54258000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -1442027836
tempLocation created <+43.57980000, -79.61713000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -680604989

tempLocation created <+43.55260000, -79.58553000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -1882725832
tempLocation created <+43.58191000, -79.71417000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 947780311

Replacing shortest distance value from 629486151 to 1089496493

tempLocation created <+43.65530000, -79.41298000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 1561068529

tempLocation created <+43.64854000, -79.38776000> +/- 0.00m (speed -1.00 mps / course -1.00) 
tempDistance  was calculated at 466814513

tempLocation created <+43.47690000, -80.52500000> +/- 0.00m (speed -1.00 mps / course -1.00) 
tempDistance  was calculated at -1816387020
Replacing shortest distance value from 947780311 to 1089489801
Received memory warning. Level=1

我现在的问题是功能正常,我试图了解这些值是什么意思?例如,分配给最短距离和温度距离的值是巨大的数字,但它们代表什么?我怎么知道这个功能正常。我不明白我希望能够使用这个最短距离的值来设置我的MKMapview,我不会用下面的行替换为与用户位置的cetercoord最短距离相对应的值

MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance(centerCoord, 15000, 15000); 

请帮忙。

1 个答案:

答案 0 :(得分:3)

CLLocationDistance属于double类型,因此您需要使用%f代替%d(用于整数)。这可能就是为什么你会看到非常大的毫无意义的数字。

假设shortestDistance也被声明为CLLocationDistance,请在记录时也这样做。

CLLocationDistance以米为单位,这也是MKCoordinateRegionMakeWithDistance对最后两个参数的期望值,因此该行可以是:

MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance
    (centerCoord, shortestDistance, shortestDistance); 

另一个不相关的事情:

您有内存泄漏:您需要在致电tempLocation后释放distanceFromLocation