2013-05-07 148 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