マップビューに注釈を追加しようとしていますが、追加できません。誰かが私が間違っている場所を教えてもらえますか? コード:マップを追加するコード:
mapview = [[MKMapView alloc] initWithFrame:CGRectMake(10, 175, 300, 268)];
mapview.delegate = self;
mapview.userInteractionEnabled = TRUE;
[mapview setZoomEnabled:TRUE];
[mapview setScrollEnabled:TRUE];
mapview.showsUserLocation = TRUE;
[self.view addSubview:mapview];
// 別のメソッドで注釈を追加します。
for (id annotation in mapview.annotations) { [mapview removeAnnotation:annotation]; }
for(int i =0;i<arrEventList.count;i++){
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [[NSString stringWithFormat:@"%@",[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"lat"]] floatValue];
theCoordinate.longitude = [[NSString stringWithFormat:@"%@",[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"lng"]] floatValue];
MyLocation *annotation = [[[MyLocation alloc] initWithName:[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"event"] address:[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:@"place"] coordinate:theCoordinate] autorelease];
[mapview addAnnotation:annotation];
//[annotations addObject:myAnno.annotation];
}
// デリゲート メソッド:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:@"annotation.png"];//here we use a nice image instead of the default pins
return annotationView;
}
return nil;
}
上記のデリゲート メソッドは、自己位置情報に対してのみ呼び出されますが、追加したい他の注釈に対しては呼び出されません。
誰かが私の間違いを指摘できますか
@interface MyLocation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
CLLocationCoordinate2D _coordinate;
}
@property (copy) NSString *name;
@property (copy) NSString *address;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
@end
//Mylocation class:
@implementation MyLocation
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]])
return @"Unknown charge";
else
return _name;
}
- (NSString *)subtitle {
return _address;
}