我发现了一些奇怪的行为,只需点击MKPointAnnotation
。
我像这样创建我的图钉:
MKPointAnnotation *activeTRPoint = [[MKPointAnnotation alloc] init];
CLLocation *coord = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
activeTRPoint.coordinate = coord.coordinate;
activeTRPoint.title = @"Title";
activeTRPoint.subtitle = @"subtitle";
//Added tag
[map addAnnotation:activeTRPoint];
[[map viewForAnnotation:activeTRPoint] setTag:1];
UIImage *im = [UIImage imageNamed:@"pin_icon.png"];
[[map viewForAnnotation:activeTRPoint] setImage:im];
因此,当我长按指针时,它会变为红色引脚(默认)。你知道为什么会这样吗?
更新:添加了viewForAnnotation方法以供进一步调查
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{
if(annotation != map.userLocation){
// This is not the users location indicator (the blue dot)
MKAnnotationView *view = [map dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
BOOL isCustomPin = [subtitles containsObject:((MKPointAnnotation*)annotation).subtitle];
if (!view && isCustomPin == NO) {
// Creating a new annotation view, in this case it still looks like a pin
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
annView.pinColor = MKPinAnnotationColorGreen;
view = annView;
view.canShowCallout = YES; // So that the callout can appear
UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[btnViewVenue addTarget:self action:@selector(pinTouched:) forControlEvents:UIControlEventTouchUpInside];
view.rightCalloutAccessoryView = btnViewVenue;
view.enabled = YES;
view.canShowCallout = YES;
view.multipleTouchEnabled = NO;
}else{
UIImage *im = [UIImage imageNamed:@"pin_icon.png"];
[view setImage:im];
}
return view;
}else{
mapView1.userLocation.title = [Lang get:@"USER_CURRENT_LOCATION"];
mapView1.userLocation.subtitle = @"";
[mapView1 setTag:999];
return nil;
}
}
答案 0 :(得分:2)
在显示的viewForAnnotation
委托方法中,实际创建的唯一类型的注释视图(alloc + init)是MKPinAnnotationView
。
当isCustomPin
为YES
时,代码为 从不 实际上会创建MKAnnotationView
,因此最终会引用view
那个:
nil
,因为dequeue
没有返回任何内容,或MKPinAnnotationView
,因为这是有史以来唯一的视图类型,而且dequeue会返回之前使用过的那种。所以当isCustomPin
为YES
时:
nil
视图,地图视图将其解释为“显示默认红色图钉”(基本上是红色MKPinAnnotationView
)或MKPinAnnotationView
。在任何情况下,代码始终返回 MKPinAnnotationView
,地图视图通常会忽略image
属性,并根据{{显示默认的图片图片1}}。
要解决此问题,您必须将“自定义图像”引脚的创建和重用与“绿色引脚”分开。例如:
pinColor
一些单独的,可能不相关的问题:
BOOL isCustomPin = [subtitles containsObject:((MKPointAnnotation*)annotation).subtitle];
if (isCustomPin)
{
//Put dequeue and alloc+init of MKAnnotationView here.
//NOTE: Use a DIFFERENT re-use identifier for "custom image" pins.
//Eg. Use "myCustomIdentifier" -- not "myAnnotationIdentifier"
}
else
{
//Put dequeue and alloc+init of MKPinAnnotationView here.
//NOTE: Use a DIFFERENT re-use identifier from "custom image" pins.
//That is, use "myAnnotationIdentifier" -- not "myCustomIdentifier".
}
的逻辑看起来有问题。最好创建一个自定义注释类,并检查isCustomPin
是否属于该类型,而不是在某个外部数组中查找注释。annotation
属性设置为当前annotation
,否则视图将使用以前的注释属性。annotation
进行标识,并且在MKUserLocation
方法中,您可以使用pinTouched:
的{{1}}属性获取所选注释。
顺便说一下,您还应该在调用selectedAnnotations
后删除图像设置。