1

我正在使用以下代码向 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
4

1 回答 1

2

As AnnaKarenina points out, you should set the image of your annotation view inside the viewForAnnotation method.

This may be somewhat "working" in iOS 6, but surely not completely. You're trying to set the annotation view's image before the annotation has been added to the map (and, thus, before the annotation view for this annotation has even been created). At best, you're setting the annotation view image for the prior annotation, not the one you think you are.

If your various annotations need to have unique images, go ahead and add a imageName property to your annotation, and then you can have your viewForAnnotation set the annotation view's image using that property. That's where you should be creating the UIImage for the annotation view. Frankly, I'd suggest you remove the annView ivar, to avoid the temptation to try to access the annotation view outside of viewForAnnotation.

By the way, the MapViewAnnotation class (referred to elsewhere as MyAnnotation) has lots of little issues of its own (e.g. your init is not calling super, you've defined the strings to be copy parameters but init is not doing that (such as title = [ttl copy]), its now advised that you don't explicitly create the ivars that back your properties, etc.), but those are unrelated to the broader issues regarding your annotation view images.

于 2013-09-26T16:50:35.480 回答