Hemang
Hemang

Reputation: 1234

How to add UIImage in MKMapView in iPhone?

I want to add a UIImage in MKMapview.

i.e.: One a particular location, I want to add an image called "apple.jpg".

I don't know how to do that.

As I have already added a draggable pin as an annotation. But I don't know if I can add multiple images or not.

Upvotes: 1

Views: 3060

Answers (4)

Paras Joshi
Paras Joshi

Reputation: 20541

For add image as a Part of MKMapView see this Tutorial...

i.ndigo mkmapview

and for add Image as a Pin on MapView then use this bellow code...

MKAnnotationView *annotationView = [[[MKAnnotationView alloc] init];

annotationView.image = [UIImage imageNamed:@"apple.png"];
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView.tag = 101;

    annotationView.canShowCallout = YES;
 [yourMapView addAnnotation:annotationView];

Upvotes: 5

Nitin Gohel
Nitin Gohel

Reputation: 49730

This is working code and i already done with below code no necessory image is .png , .jpg or .gif its any formate you can use it

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
           MKPinAnnotationView *pinAnnotation = nil;
           NSString *defaultPinID = @"myPin";
           pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
                            if ( pinAnnotation == nil )
                                pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

           pinAnnotation.image = [UIImage imageNamed:@"marker_postoffice.png"];
           pinAnnotation.annotation = annotation;
           pinAnnotation.canShowCallout = YES;
           UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];



          [infoButton addTarget:self action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
          pinAnnotation.rightCalloutAccessoryView = infoButton;





       // now you can set diff image by  [annotation title] you  can set diff image with if else condition like below code


        if([[annotation title] isEqualToString:objAppDelegate.OfficePinTitle]) 
         {   

                        pinAnnotation.image = [UIImage imageNamed:@"marker_postoffice.png"];
                        pinAnnotation.annotation = annotation;
                        pinAnnotation.canShowCallout = YES;
                        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

                        infoButton addTarget:self
                                                       action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside];

                                        pinAnnotation.rightCalloutAccessoryView = infoButton;

         }
         else
         {

                        pinAnnotation.image = [UIImage imageNamed:@"marker_red_postoffice.png"];
                        pinAnnotation.canShowCallout = YES;
                        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

                        [infoButton addTarget:self action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside];

                         pinAnnotation.rightCalloutAccessoryView = infoButton;
         }


}

Upvotes: 1

Viral Narshana
Viral Narshana

Reputation: 1877

Your image must be in png format see the code below.

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

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
   pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"] autorelease];
   pinView.image = [UIImage imageNamed:@"apple.png"];
   pinView.canShowCallout = YES;
 }
}

Upvotes: 1

whiteagle
whiteagle

Reputation: 1158

as @Kassem Bagher mentions, you need to create custom MKAnnotationView , check whatever tutorial (example)

Upvotes: 2

Related Questions