gpt4 book ai didi

ios - map 注释不会在 iOS 7 中显示

转载 作者:行者123 更新时间:2023-11-29 10:54:28 25 4
gpt4 key购买 nike

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

最佳答案

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

这在 iOS 6 中可能有些“有效”,但肯定不是完全有效。您试图在将注释添加到 map 之前(因此,甚至在创建此注释的注释 View 之前)设置注释 View 的 image 。充其量,您正在为之前的注释设置注释 View image,而不是您认为的那个。

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

顺便说一句,MapViewAnnotation 类(在其他地方称为 MyAnnotation)有很多自己的小问题(例如您的 init没有调用 super,您已经将字符串定义为 copy 参数,但是 init 没有这样做(例如 title = [ttl copy]),现在建议您不要明确创建支持您的属性等的 ivars),但这些与有关注释 View 图像的更广泛问题无关。

关于ios - map 注释不会在 iOS 7 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19032911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com