每个引脚不同的自定义图像

这可能听起来像一个普遍的问题,但我只find答案,如果我想我所有的针脚相同的图像,我不知道。

这就是我现在正在工作:

我有我所有的位置在一个地点arrays(自定义类与长,拉特,名称,引脚名称)。

在viewdidLoad我循环该数组并创build我的引脚与发现的每个对象,请参阅以下代码:

for(int i = 0 ; i<[_locationList count] ; i++) { Location *item = [_locationList objectAtIndex:i]; CLLocationCoordinate2D coordinate; coordinate.latitude = item.locationLatitude; coordinate.longitude = item.locationLongitude; NSString *title = item.locationName; CustomAnnotation* ann = [CustomAnnotation new]; ann.name = title; ann.coordinate = coordinate; ann.pinName = [NSString stringWithFormat:@"pin%i.png",item.idPin]; [self.map addAnnotation:ann]; } 

这非常简单,部分来自CustomAnnotation类,它是以下代码:

 @interface CustomAnnotation : MKPointAnnotation <MKAnnotation>{ } @property (nonatomic, retain) NSString *name; @property (nonatomic, retain) NSString *description; @property (nonatomic, retain) NSString *pinName; @end 

这一切都来自我在互联网上看到的东西,我有点相信这一切都是正确的。

在我看来,我还在创造非常经典的针脚,他们只有一个属性(pinName),这就是为什么它来自自定义类。

现在,在viewForAnnotation中,我绝对没有想法如何告诉它获取该pinName并使用它。

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { // If it's the user location, just return nil. if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // Handle any custom annotations. if ([annotation isKindOfClass:[MKPointAnnotation class]]) { // Try to dequeue an existing pin view first. MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; if (!pinView) { // If an existing pin view was not available, create one. pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; pinView.canShowCallout = YES; pinView.image = // I wanted to write annotation.pinName But it's just not that. pinView.calloutOffset = CGPointMake(0, 32); }else { pinView.annotation = annotation; } return pinView; } return nil; } 

我错过了什么? 我明显做错了,但我jsut无法弄清楚,我还是很困惑与MKAnnotationsView和MKPointAnnotation MKPInAnnotation之间的差异,… … –

更多信息:引脚名称是“pinX.png”,X是1到12之间的数字。我只是想使用该名称,以便程序可以在图片所在的资源中find它。

由于您的注释types为CustomAnnotation ,因此检查注释是否属于该types而不是MKPointAnnotation会更准确。

然后,将annotation参数转换为您的自定义类将使您可以轻松访问其中的自定义属性,如下所示:

 CustomAnnotation *ca = (CustomAnnotation *)annotation; pinView.image = [UIImage imageNamed:ca.pinName]; 

但是,由于每个注释的图像可能不同,所以应该在注释视图创build出列(不仅在if (!pinView)块中)之后进行设置,否则注释可能会最终重新使用视图另一个注释不再可见,并显示错误的图像)。

更新的方法如下所示:

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { // If it's the user location, just return nil. if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // Handle any custom annotations. if ([annotation isKindOfClass:[CustomAnnotation class]]) { // Try to dequeue an existing pin view first. MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; if (!pinView) { // If an existing pin view was not available, create one. pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; pinView.canShowCallout = YES; pinView.calloutOffset = CGPointMake(0, 32); //NOTE: //If the calloutOffset needs to be different for each image, //then this line should also be set below with the image. } else { pinView.annotation = annotation; } //Set image on view AFTER we have a new or dequeued view //because image is based on each annotation... CustomAnnotation *ca = (CustomAnnotation *)annotation; pinView.image = [UIImage imageNamed:ca.pinName]; //Might want to check that the UIImage is not nil //in case pinName is invalid since that would result in //an invisible annotation view. If the UIImage is nil, //set pinView.image to some default image. return pinView; } return nil; } 

有关MapKit类之间的差异,请参阅:

  • 我应该使用MKAnnotation,MKAnnotationView还是MKPinAnnotation? 。 即使这个问题被标记为MonoTouch,它仍然适用。

  • MKMapView,animateDrop? 也可能有所帮助。