1
因此,我已經成功地將一個註釋添加到具有自定義圖像的地圖視圖中,但是如果我添加第二個註釋,它只會拒絕顯示在地圖上。添加多個註解到地圖視圖不起作用
下面是相關代碼:
- (void)viewDidLoad {
[super viewDidLoad];
//mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.delegate = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Location Info" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(showCurrentLocationButtonTapped:)];
[self loadOverlays];
//TEST LOAD TWO ANNOTATIONS
//MKPointAnnotation *testAnnot = [[MKPointAnnotation alloc] init];
ArboretumAnnotation *arboAnnot = [[ArboretumAnnotation alloc] init];
CLLocationCoordinate2D workingCoord;
workingCoord.latitude = /*some coord*/;
workingCoord.longitude = /*some coord*/;
[arboAnnot setCoordinate:workingCoord];
[arboAnnot setTitle:@"Test Title"];
[arboAnnot setSubtitle:@"Test Subtitle"];
[mapView addAnnotation:arboAnnot];
[arboAnnot release];
ArboretumAnnotation *arboAnnot2 = [[ArboretumAnnotation alloc] init];
CLLocationCoordinate2D workingCoord2;
workingCoord2.latitude = /*some coord*/;
workingCoord2.longitude = /*some coord*/;
[arboAnnot2 setCoordinate:workingCoord2];
[arboAnnot2 setTitle:@"Test Title2"];
[arboAnnot2 setSubtitle:@"Test Subtitle2"];
[mapView addAnnotation:arboAnnot2];
[arboAnnot2 release];
//...
}
//...
- (ArboretumAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:MKUserLocation.class]) {
//it's the built-in user location annotation(blue dot)
return nil;
}
NSString *annotIdentifier = @"annotation";
MKPinAnnotationView *recycledAnnotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotIdentifier];
if (!recycledAnnotationView) {
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:annotIdentifier] autorelease];
UIImage *iconImage = [UIImage imageNamed:@"arboretum.png"];
CGRect resizeRect;
resizeRect.size = iconImage.size;
CGSize maxSize = CGRectInset(self.view.bounds,
10.0f,
10.0f).size;
maxSize.height -= self.navigationController.navigationBar.frame.size.height + 40.0f;
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height/resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width/resizeRect.size.height * maxSize.height, maxSize.height);
customPinView.image = iconImage;
//customPinView.image.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder);
customPinView.opaque = NO;
//customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.canShowCallout = YES;
return customPinView;
} else {
recycledAnnotationView.annotation = annotation;
}
return recycledAnnotationView;
}
ArboretumAnnotation頭:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface ArboretumAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
UIImage *image;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@end
ArboretumAnnotation實現:
#import "ArboretumAnnotation.h"
@implementation ArboretumAnnotation
@synthesize title;
@synthesize subtitle;
@synthesize image;
@synthesize coordinate;
-init
{
return self;
}
-initWithCoordinate:(CLLocationCoordinate2D)inCoord
{
coordinate = inCoord;
return self;
}
- (CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = /*some coord*/;
theCoordinate.longitude = /*some coord*/;
return theCoordinate;
}
@end
我不知道我做錯了,第一個註釋我添加顯示器,但第二個不顯示。有任何想法嗎?提前致謝!