我试图将不同的图像添加到不同的注释视图中,换句话说,我想要一张独特的图片对应于每个独特的引脚。这是我正在尝试的:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"welcome into the map view annotation");
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorPurple;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
if (CLLocationCoordinate2D == theCoordinate1) {
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Jeff.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
[profileIconView release];
}else if(CLLocationCoordinate2D = theCoordinate2) {
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Pierce.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
[profileIconView release];
}
我所写的行出现错误
if (CLLocationCoordinate2D == theCoordinate1) {
我不太确定出了什么问题,也无法找出另一种方法来识别单个注释。任何帮助是极大的赞赏!!
该行给出了一个错误,因为CLLocationCoordinate2D
是一种结构体并且theCoordinate1
我认为是一个类型的变量CLLocationCoordinate2D
。你无法比较两者。
您想要做的是将请求视图的当前注释的坐标与中的坐标进行比较theCoordinate1
。为此,如果必须的话,您需要这样的东西:
if ((annotation.coordinate.latitude == theCoordinate1.latitude)
&& (annotation.coordinate.longitude == theCoordinate1.longitude)) {
但是,我不建议以这种方式比较浮点数,即使它有时“有效”。如果必须比较坐标,请使用 CLLocation 的distanceFromLocation:
方法并查看两者之间的距离是否低于某个阈值(例如 10.0 米)。
检查注释是否是您要查找的注释的另一种方法是保留对注释本身的引用(您传递给注释的注释)addAnnotation:
方法)然后你可以做if (annotation == theAnnotation1)
.
如果您不想保留对注释的引用,您还可以检查注释的标题是否是您要查找的标题(if ([annotation.title isEqualToString:@"Jeff"])
).
最好的方法是将自定义属性(最好是 int)添加到自定义注释类并在 viewForAnnotation 中检查该属性。
其他一些不相关的事情:
- 不执行 addTarget,而是处理地图视图自己的按钮按下操作
calloutAccessoryControlTapped
委托方法将提供对注释的引用(请参阅如何查找哪个注释发送了showDetails?例如)。
- 您有一条“出队”评论,但您没有这样做。推荐使用
dequeueReusableAnnotationViewWithIdentifier
在 viewForAnnotation 中重用视图(参见EXC_BAD_ACCESS 与 MKPinAnnotationView例如)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)