Reputation: 3824
I added MKAnnotation in my mapview using the following code.
Flag *flag = [[Flag alloc] init];
flag.title = @"Golf Course";
flag.subtitle = @"Green";
flag.coordinate =CLLocationCoordinate2DMake(28.457394,77.108667);
[mapView addAnnotation:flag];
and am setting image like
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation
{
if(([annotation isKindOfClass:[Flag class]]))
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"PinId1";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin1"] autorelease];
//pinView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin1"];
pinView.canShowCallout = YES;
pinView.draggable = YES;
[pinView setImage:[UIImage imageNamed:@"flag2.png"]];
}
else
{
[mapView.userLocation setTitle:@"I am here"];
}
}
return pinView;
}
but am not getting the image for MKAnnoation.What is wrong am doing?Please help me.
Upvotes: 2
Views: 3937
Reputation: 5418
Where you have to set image?is it on callouts? if yes replace [pinView setImage:[UIImage imageNamed:@"flag2.png"]];
with pinView.leftCalloutAccessoryView = [[ UIImageView alloc]initWithImage:[UIImage imageNamed:@"flag2.png"]];
Upvotes: 0
Reputation: 4527
I have the same issue. Now using the working version as given below.
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[Annotation class]])
{
static NSString* SFAnnotationIdentifier = @"SFAnnotationIdentifier";
MKPinAnnotationView* pinView =
(MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
if (!pinView)
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
UIImage *flagImage = [UIImage imageNamed:@"map_pin.png"];
CGRect resizeRect;
resizeRect.size = flagImage.size;
CGSize maxSize = CGRectInset(self.view.bounds,
10.5,
10.5).size;
maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0;
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
annotationView.opaque = NO;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Annotation *annoterObject = (Annotation *) annotation;
rightButton.tag = annoterObject.annoteCount;
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
This is the working code.
Upvotes: 6