嗨我的应用程序中有一个mapAnnotations数组。每个注释对象都有一个地址和一个坐标。我正在使用我的位置管理器来查找用户的当前位置,它是一个CLLocationDistance对象。然后,我试图迭代mapAnnotations数组,并将用户的位置与每个注释的位置坐标进行比较,以找到接近用户的位置坐标。 下面是代码:比较CLLocationDistance的两个实例来缩小较小的一个
-(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);
请帮忙。
非常小的事情 - 但CLLocation不是一个对象,因此它不是一个“实例”。我已经改进了你的代码格式 - 请不要在粘贴代码时使用标签 - 使用空格,它在SO上效果会更好。祝你好运! – makdad 2011-05-23 01:25:43
'CLLocationDistance'是'double'的一个typedef。你可以用'%lf's替换'%d's并发布输出吗? – 2011-05-23 01:33:36
感谢您的帮助。 – banditKing 2011-05-23 02:36:33