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

Posted

技术标签:

【中文标题】无法在 iPhone 的地图上显示自定义注释【英文标题】:unable to display custom Annotations on the map in iPhone 【发布时间】:2012-04-25 12:26:49 【问题描述】:

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

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】:

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

【讨论】:

以上是关于无法在 iPhone 的地图上显示自定义注释的主要内容,如果未能解决你的问题,请参考以下文章

地图注释显示不正确的自定义图钉图像

如何在 MKMapView 上显示自定义注释?

MapKit 自定义注释被添加到地图,但在地图上不可见

自定义地图注释标注 - 如何控制宽度

自定义地图所需的 MKMapView“合法”标签?

MapKit iPhone 开发;当前位置和注释