Daniel Fernandez Daniel Fernandez - 12 days ago 4
iOS Question

Show different images in leftCalloutAccessoryView Map Xcode

I am trying to get different images in the leftCalloutAccessoryView in each map pin on Xcode (objetive-c). I was looking for other responses on the internet but I didn't find this one (only how to change the pin image). But I want to change the image inside the pin, on the left of the pin title. Can someone help me? Thank you very much.

This is my RestMapPin.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface RestMapPin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *image;

@end


This is my RestMapPin.m:

#import "RestMapPin.h"

@implementation RestMapPin
@synthesize coordinate, title, subtitle, image;

@end


This is my MapViewController.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController {
MKMapView *mapView;
}

@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;

@end


This is my MapViewController.m:

#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"
#import "RestViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

- (void)viewDidLoad {
[super viewDidLoad];

_barButton.target = self.revealViewController;
_barButton.action = @selector(revealToggle:);

[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

/*Babelia*/
MKCoordinateRegion region_Babelia = { {0.0, 0.0}, {0.0, 0.0}};
region_Babelia.center.latitude = 40.4234778;
region_Babelia.center.longitude = -3.686283000000003;
region_Babelia.span.longitudeDelta = 0.1f;
region_Babelia.span.latitudeDelta = 0.1f;
[mapView setRegion:region_Babelia animated:YES];

RestMapPin *ann_Babelia = [[RestMapPin alloc] init];
ann_Babelia.title = @"Babelia";
ann_Babelia.subtitle = @"Barrio de Salamanca";
ann_Babelia.coordinate = region_Babelia.center;
[mapView addAnnotation:ann_Babelia];

/*Bacira*/
MKCoordinateRegion region_Bacira = { {0.0, 0.0}, {0.0, 0.0}};
region_Bacira.center.latitude = 40.43375390000001;
region_Bacira.center.longitude = -3.699036299999989;
region_Bacira.span.longitudeDelta = 0.1f;
region_Bacira.span.latitudeDelta = 0.1f;
[mapView setRegion:region_Bacira animated:YES];

RestMapPin *ann_Bacira = [[RestMapPin alloc] init];
ann_Bacira.title = @"Bacira";
ann_Bacira.subtitle = @"Chamberí";
ann_Bacira.coordinate = region_Bacira.center;
[mapView addAnnotation:ann_Bacira];
}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pin2@2x.png"];
}
else {
//[mapView.userLocation setTitle:@"I am here"];
}

UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin2@2x.png"]];
pinView.leftCalloutAccessoryView = leftIconView;
leftIconView.layer.cornerRadius = 6;
leftIconView.clipsToBounds = YES;
leftIconView.layer.borderWidth = 1;

return pinView;
}

@end


I know that is something similar to the code in the viewForAnnotation method:

UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin2@2x.png"]];
pinView.leftCalloutAccessoryView = leftIconView;


But I don't know how to change it to be different for each map pin ("Babelia" and "Bacira").
Thank you very much for your responses!

Answer
  @interface RestMapPin : NSObject <MKAnnotation>
  {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    NSString *pincolor;
  }

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *pincolor;
@property (nonatomic, copy) NSString *image;

Give pin value

ann_Bacira. pincolor = @“red”;
ann_Babelia.pincolor  = “green”;


   -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
   {
     MKAnnotationView *pinView = nil;
     UIImage *objImage = nil;
     if(annotation != mapView.userLocation) 
    {
     static NSString *defaultPinID = @"com.invasivecode.pin";
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
     if ( pinView == nil )
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

     pinView.canShowCallout = YES;
    if([annotation.pincolor isEqualToString:@"green"])
    {
     objImage =[UIImage imageNamed:@“image1@2x.png"]
    }
    else if([annotation.pincolor isEqualToString:@"red"])
    {
    objImage =[UIImage imageNamed:@“image2@2x.png"]
    }

    pinView.image = objImage;
    }
   else {
    //[mapView.userLocation setTitle:@"I am here"];
     }
    return pinView;
    }
Comments