Reputation: 2171
I am trying to add an image to my MKAnnotation. I have the code below. How would I add @"map_icon.png" to this? Any help would be great. Thank you!
mapView.m file:
// retrieve latitude and longitude from the dictionary entry
location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];
// create the annotation
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"name"]
andCoordinate:location];
MapViewAnnotation.m file
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d
{
title = ttl;
coordinate = c2d;
return self;
}
Thank you!
Upvotes: 2
Views: 605
Reputation: 437372
You do this by responding to mapView:viewForAnnotation
, e.g.:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKAnnotationView* annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"MyCustomAnnotation"];
annotationView.image = [UIImage imageNamed:@"map_icon.png"];
annotationView.canShowCallout = YES;
// If you've added the QuartzCore.framework to your project, you can also add a shadows/borders/etc.
// For example, this code adds a shadow
//
// [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
// [annotationView.layer setShadowOpacity:0.8f];
// [annotationView.layer setShadowRadius:5.0f];
// [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
// [annotationView setBackgroundColor:[UIColor whiteColor]];
return annotationView;
}
// Note, if the annotation isn't your custom annotation (e.g. a `MKUserAnnotation`)
// then return nil so default behavior will take place.
return nil;
}
For more information, please see Adding Annotations to a Map section of the Location Awareness Programming Guide.
Upvotes: 4
Reputation: 276
Implement the MKMapViewDelegate in your mapView Controller
myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
myMapView.delegate = self;
Implement the method in your mapView Controller
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
If you want a custom image to replace the default pin
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
[annotationView setImage:[UIImage imageNamed:@"map_icon.png"]];
return annotationView;
}
return nil;
}
If you want an image in the callout bubble
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
UIImageView *pin = [[UIImageView alloc] initWithFrame:CGRectMake(-5, -5, 10, 10)];
[pin setImage:[UIImage imageNamed:@"map_icon.png"]];
annotationView.leftCalloutAccessoryView = pin;
return annotationView;
}
return nil;
}
Upvotes: 2