I have an NSDictionary of about 2000 locations with lat and long and I am dropping pins on map based on if they are in the visible map region.
Currently every time the pan the map I simply loop through my dictionary and calculate the distance to see if the location is visible, if so drop a pin.
CLLocationCoordinate2D centre = [self.map centerCoordinate];
CLLocation *mapCenter =[[CLLocation alloc] initWithLatitude: centre.latitude longitude: centre.longitude];
for (int i=0; i < [self.dealersSource count]; i++) {
CLLocation *d = [[CLLocation alloc] initWithLatitude: [[[self.dealersSource objectAtIndex:i] valueForKey:@"lat"] floatValue]
longitude: [[[self.dealersSource objectAtIndex:i] valueForKey:@"long"] floatValue]];
CLLocationDistance distance = [d distanceFromLocation:mapCenter];
float dist =(distance/1609.344);
if (dist <= radius && dist !=0) {
// this will be visible on the map, add to list of annotations
}
}
This works but seems pretty inefficient and can be slow on older iPads - especially if more and more locations get added to this list. I would like to be able to use some sort of NSPredicate to filter my initial list before I start looping though them.