I am trying to assign two custom annotations, one called \"arsenal.png\" and one called \"chelsea.png\"
Using apple mapkit framework, xcode 7.
Need assistanc
You do need to implement the viewForAnnotation delegate method to show images for your annotations instead of the standard red pin.
In that delegate method, you need to create and return an MKAnnotationView with its image property set to the image you want for the annotation.
You will get a reference to the annotation the map view is asking for a view for and using the annotation's properties, you set the image accordingly.
You could base it on the annotation's title value but I'd suggest the following approach instead:
Add a imageName property of type NSString to your Annotation class. Set this property to the image name to use for the annotation before you call addAnnotation on it. For example:
myAnn.title = @"Arsenal FC";
myAnn.subtitle = @"The Gunners";
myAnn.imageName = @"arsenal";
[locations addObject:myAnn];
In the viewForAnnotation delegate method, check if the annotation is of type Annotation and then use its imageName property to set the annotation view's image property:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[Annotation class]])
{
static NSString *reuseId = @"ann";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
else
{
av.annotation = annotation;
}
Annotation *ann = (Annotation *)annotation;
av.image = [UIImage imageNamed:ann.imageName];
return av;
}
//return nil (default view) if annotation is not our custom type
return nil;
}
Also, don't forget to set the map view's delegate property otherwise the delegate method won't get called. In Interface Builder, connect the delegate outlet to the view controller.
You can use viewForAnnotation delegate method for creating custom annotation.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
See the below tutorials
http://www.codigator.com/tutorials/advanced-mapkit-tutorial-for-ios-custom-callout/
http://ios-funda.blogspot.in/2012/08/custom-annotations-example.html
EDIT
You need to set the delegate in viewDidLoad
self.myMapView.delegate = self
Now in .h
@interface TrucksViewController : UIViewController<MKMapViewDelegate>
Then implement the delegate method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *defaultPinId = @"Pin";
CustomAnnotation *pinView = (CustomAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinId];
if (pinView == nil) {
pinView = [[CustomAnnotation alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinId];
//pinView.pinColor = MKPinAnnotationColorRed;
//pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
//NSLog(@"%f",[annotation coordinate].latitude);
}
else {
pinView.annotation = annotation;
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
btn.frame = CGRectMake(275, 27, 30, 30);
//Adding a navigation inside a callout view
btn.tag = [sid intValue];
//NSLog(@"%i",btn.tag);
[btn addTarget:self action:@selector(YOUR_SELECTOR) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = btn;
return pinView;
}
in CustomAnnotation.m add the below methods
-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil) {
CGRect frame = self.frame;
frame.size = CGSizeMake(46.0, 49.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(-5, -5);
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[[UIImage imageNamed:@"locatorico"] drawInRect:rect];
}