Comparing two instances of CLLocationDistance to fid the smaller one
Hi I have an array of mapAnnotations in my app. Each annotation object has an address and a coordinate. I am using my location manager to find out the user's current location which is a CLLocationDistance object. Then I am trying to iterate over the mapAnnotations array and comparing the user's location with the location coordinates of each annotation to find the one thats closes to the user. Here is the code:
-(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];
}
开发者_JAVA技巧//==============================================================================
-(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;
}
}
}
The method works fine and here is the console output:
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
My question is now that the function is working, Im trying to understand what the values mean? For instance the values assigned to shortest distance and tempdistance are huge numbers but what do they represent? How do I know the function is working fine. I dont understand. I want to be able to use this value of shortest distance to set my my MKMapview here, I wont to replace the line below with the value which corresponds to the shortest distance from the cetercoord which is the user's location
MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance(centerCoord, 15000, 15000);
Please help.
CLLocationDistance
is of type double
so you need to use %f
instead of %d
(which is for ints). This is probably why you are seeing very large meaningless numbers.
Assuming shortestDistance
is also declared as a CLLocationDistance
, do the same when logging that as well.
CLLocationDistance
is in meters which is also what MKCoordinateRegionMakeWithDistance
expects for the last two parameters so that line could be:
MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance
(centerCoord, shortestDistance, shortestDistance);
Another unrelated thing:
You have a memory leak: you need to release tempLocation
after the call to distanceFromLocation
.
精彩评论