嗨,我的应用程序中有一组 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);
请帮忙。