2013-05-07 114 views
0

我已经创建了在我的地图上完美显示的注释,但我不确定如何实现leftCalloutAccessoryView以显示图像。在我的注释的左边添加图像

MapViewAnnotation.h

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

@interface MapViewAnnotation : NSObject <MKAnnotation> 

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

@end 

MapViewAnnotation.m

#import "MapViewAnnotation.h" 

@implementation MapViewAnnotation 
@synthesize coordinate, title, subtitle; 

@end 

GroundsViewController.h

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


@interface GroundsViewController : UIViewController <MKMapViewDelegate> 
{ 
    MKMapView *mapView; 
} 

@property (strong, nonatomic) IBOutlet UISegmentedControl *segment; 

- (IBAction)changeSeg:(id)sender; 
@end 

GroundsViewController.m

#import "GroundsViewController.h" 
#import "MapViewAnnotation.h" 

@interface GroundsViewController() 


@end 

// Centre in on Northern Ireland 

#define Northern_Ireland_Latitude 54.629338; 
#define Northern_Ireland_Longitude -6.668701; 

//Span 

#define The_Span 2.00f; 


// Premiership 

#define Ballymena_Latitude 54.870105; 
#define Ballymena_Longitude -6.265076; 


// Championship 1 

#define Ards_Latitude 54.651629; 
#define Ards_Longitude -5.684478; 

// Championship 2 

#define Annagh_Latitude 54.411372; 
#define Annagh_Longitude -6.440355; 


@implementation GroundsViewController 

@synthesize segment; 

- (IBAction)changeSeg:(id)sender { 
    if (segment.selectedSegmentIndex == 0) { 
     mapView.mapType = MKMapTypeStandard; 
    } 

    if (segment.selectedSegmentIndex == 1) { 
     mapView.mapType = MKMapTypeSatellite; 
    } 

    if (segment.selectedSegmentIndex == 2) { 
     mapView.mapType = MKMapTypeHybrid; 
    } 

} 

// When the view loads 
- (void)viewDidLoad 
{ 
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 
    [self.view insertSubview:mapView atIndex:0]; 

    // Create the region 

    MKCoordinateRegion myRegion; 

    // Center 

    CLLocationCoordinate2D center; 
    center.latitude = Northern_Ireland_Latitude; 
    center.longitude = Northern_Ireland_Longitude; 

    // Create the Span 

    MKCoordinateSpan span; 
    span.latitudeDelta = The_Span; 
    span.longitudeDelta = The_Span; 

    myRegion.center = center; 
    myRegion.span = span; 

    // Set our map view 

    [mapView setRegion:myRegion animated:YES]; 


    NSMutableArray *locations = [[NSMutableArray alloc] init]; 
    CLLocationCoordinate2D location; 
    MapViewAnnotation *myAnn; 

    //Premiership 

    // Pin to show Ballymena United F.C. 
    myAnn = [[MapViewAnnotation alloc] init]; 
    location.latitude = Ballymena_Latitude; 
    location.longitude = Ballymena_Longitude; 
    myAnn.coordinate = location; 
    myAnn.title = @"Ballymena United F.C."; 
    myAnn.subtitle = @"The Showgrounds"; 
    [locations addObject:myAnn]; 


    // Championship 1 

    // Pin to show Ards F.C. 
    myAnn = [[MapViewAnnotation alloc] init]; 
    location.latitude = Ards_Latitude; 
    location.longitude = Ards_Longitude; 
    myAnn.coordinate = location; 
    myAnn.title = @"Ards F.C."; 
    myAnn.subtitle = @"Clandeboye Park"; 
    [locations addObject:myAnn]; 


    // Championship 2 

    // Pin to show Annagh United F.C. 
    myAnn = [[MapViewAnnotation alloc] init]; 
    location.latitude = Annagh_Latitude; 
    location.longitude = Annagh_Longitude; 
    myAnn.coordinate = location; 
    myAnn.title = @"Annagh United F.C."; 
    myAnn.subtitle = @"Tandragee Road"; 
    [locations addObject:myAnn]; 


    [self->mapView addAnnotations:locations]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

回答

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

static NSString *identifier = @"MyLocation"; 
if ([annotation isKindOfClass:[PlaceMark class]]) { 

    MKPinAnnotationView *annotationView = 
    (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] 
          initWithAnnotation:annotation 
          reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 


UIImageView *imvLeft = [[UIImageView alloc] init]; 
//Set Image 
    [annotationView setLeftCalloutAccessoryView:imvLeft]; 

    return annotationView; 
} 

return nil; 

}

+0

感谢您的答复Mohith,我在哪里可以添加此我的代码,并且将这个允许为每个标注不同的图像?我为每个注释上传了.png图像 – Elfuthark 2013-05-07 16:51:51

+0

您需要将其添加到GroundsViewController.m。是的,你可以为每个设置不同的图像。 – Mohith 2013-05-07 16:56:50