无法在iPhone中的地图上显示自定义注释

时间:2012-04-25 12:26:50

标签: iphone ios annotations mkmapview

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

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(mapview.annotations中的id注释){                 [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;
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。我的代码是正确的。纬度和经度的价值以某种方式互换。我的错,但我现在已经纠正了