0
votes

Below, I have created an Annotation for my map which works perfectly and shows up with the title and subtitle as it should.

But I wish to add a small image to the left of the annotation but can't figure out what I need to do to the below code to make it work.

// Annotation
CLLocationCoordinate2D poseLocation;
poseLocation.latitude = POSE_LATITUDE;
poseLocation.longitude = POSE_LONGITUDE;

Annotation *myAnnotation = [[Annotation alloc] init];
myAnnotation.coordinate = poseLocation;
myAnnotation.title = @"Pose Beauty Salon";
myAnnotation.subtitle = @"100, Moneyhaw Road";

[self.myMapView addAnnotation:myAnnotation];
2
Unrelated but you should do [[Annotation alloc] init]; instead of just [Annotation alloc];.user467105

2 Answers

2
votes

You will need to set the delegate of your myMapView first and implement the viewForAnnotation delegate method.

There you will return MKAnnotationView instance which has a property leftCalloutAccessoryView:

The view to display on the left side of the standard callout bubble. The default value of this property is nil. The left callout view is typically used to display information about the annotation or to link to custom information provided by your application. The height of your view should be 32 pixels or less.

In the leftCalloutAccessoryView, you can assign your image there. For example:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"];

    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];            
        UIImageView *imageView = //initialize your image view here
        annotationView.leftCalloutAccessoryView = imageView;
    }

    annotationView.annotation = annotation;
    return annotationView;
}

PS: Apparently you have asked similar question before here: Add image to the left of my annotations. I am not sure you need to post another question. Please try to implement this first.

0
votes
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];

myImage = [UIImage imageNamed:imagename];
CGRect cropRect = CGRectMake(0.0, 0.0,35.0, 35.0);
myImageView = [[UIImageView alloc] initWithFrame:cropRect];
myImageView.clipsToBounds = YES;
myImageView.image = myImage;
MyPin.leftCalloutAccessoryView =myImageView;
MyPin.highlighted=YES;
MyPin.canShowCallout=YES;
return MyPin;

}

It works for me , try this one