地图注释不会在iOS 7中显示

时间:2013-09-26 15:50:05

标签: ios objective-c mkmapview mkannotationview

我正在使用以下代码向mapView添加注释。问题是注释没有出现在iOS 7中,但确实出现在iOS6中

在我的viewDidLoad方法中:

for (int i=0; i<[self.listingNodesArray count]; i++) {
    MyAnnotation* annotation= [MyAnnotation new];

    CLLocationCoordinate2D coordinate;
    coordinate.latitude = [[[[self.listingNodesArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"lat"] doubleValue];
    coordinate.longitude = [[[[self.listingNodesArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"lng"] doubleValue];

    annView.image = [UIImage imageNamed:@"PIN_img.png"];// sets image for pin

    annotation.coordinate = coordinate;

    annotation.title = [[self.listingNodesArray objectAtIndex:i] objectForKey:@"title"];
    annotation.subtitle = [[[self.listingNodesArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"address"];

    NSNumber *listingIdNumber = [[self.listingNodesArray objectAtIndex:i] objectForKey:@"id"];

    annotation.catListingMapId = listingIdNumber;

    [self.topMapView addAnnotation: annotation];

}

[self.view addSubview:self.topMapView];

viewForAnnotation

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation {
    annView = nil;
    if(annotation != mapingView.userLocation)
    {

        static NSString *defaultPinID = @"";
        annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( annView == nil )
            annView = [[MKAnnotationView alloc]
                       initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;


        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        //   [rightButton addTarget:self
        //               action:@selector(showDetails:)
        //   forControlEvents:UIControlEventTouchUpInside];
        annView.rightCalloutAccessoryView = rightButton;

        annView.canShowCallout = YES;

    }

    return annView;
}

和我的注释文件:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapViewAnnotation : NSObject <MKAnnotation> {

    NSString *title;
    CLLocationCoordinate2D coordinate;

}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;

@end

    #import "MapViewAnnotation.h"

@implementation MapViewAnnotation

@synthesize title, coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {

    title = ttl;
    coordinate = c2d;
    return self;
}


@end

1 个答案:

答案 0 :(得分:2)

正如AnnaKarenina指出的那样,您应该在image方法中设置注释视图的viewForAnnotation

这可能在iOS 6中有些“有效”,但肯定不完全。您正在尝试在将注释添加到地图之前设置注释视图image(因此,甚至在创建此注释的注释视图之前)。最好的情况是,您要为先前的注释设置注释视图image,而不是您认为的注释视图。

如果您的各种注释需要具有唯一的图像,请继续向注释添加imageName属性,然后您可以使用该属性设置viewForAnnotation注释视图的图像。这就是您应该为注释视图创建UIImage的位置。坦率地说,我建议您删除annView ivar,以避免尝试访问viewForAnnotation之外的注释视图。

顺便说一下,MapViewAnnotation类(在其他地方称为MyAnnotation)有很多小问题(例如init没有调用super,您已将字符串定义为copy参数,但init未执行此操作(例如title = [ttl copy]),现在建议您不要显式创建支持您的属性的ivars等等),但这些与注释视图图像的更广泛问题无关。