0

我正在尝试在我的地图视图上添加注释,但我无法添加它们。有人可以建议我在哪里做错了吗?代码:添加地图的代码:

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;
}
4

1 回答 1

0

我发现了问题。我的代码是正确的。纬度和经度的值有些互换。我的错误,但我现在已经纠正了

于 2012-04-26T13:16:12.117 回答