我创建的注释在我的地图上完美显示,但我不确定如何实现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 :(得分: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;
}