Skip to content

Instantly share code, notes, and snippets.

@Morse-Code
Created October 28, 2012 02:43
Show Gist options
  • Save Morse-Code/3967246 to your computer and use it in GitHub Desktop.
Save Morse-Code/3967246 to your computer and use it in GitHub Desktop.
Boilerplate MKMapView annotation view creation and update based on map region.
# pragma mark -
# MapView Delegate methods
- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated
{
NSArray *oldAnnotations = mapView.annotations;
[mapView removeAnnotations:oldAnnotations];
NSArray *weatherItems = [weatherServer weatherItemsForMapRegion:mapView.region maximumCount:4];
[mapView addAnnotations:weatherItems];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
WeatherAnnotationView *annotationView =
(WeatherAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[WeatherAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.annotation = annotation;
return annotationView;
}
# pragma mark -
# MKAnnotation protocol
@interface
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@end
@implementation
- (CLLocationCoordinate2D)coordinate
{
coordinate.latitude = [self.latitude doubleValue];
coordinate.longitude = [self.longitude doubleValue];
return coordinate;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment