我正在尝试将不同的图像添加到不同的注释视图中,换句话说,我想要一个唯一的图片来对应每个唯一的图钉。这是我正在尝试的:
- (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) {
我不确定到底出了什么问题,也无法找出另一种识别单个注释的方法。非常感谢任何帮助!!
答案 0 :(得分:2)
该行给出错误,因为CLLocationCoordinate2D
是一种结构,{I}假设theCoordinate1
是CLLocationCoordinate2D
类型的变量。你无法比较两者。
您要做的是将请求查看的当前注释的坐标与theCoordinate1
中的坐标进行比较。要做到这一点,如果必须,你需要这样的东西:
if ((annotation.coordinate.latitude == theCoordinate1.latitude)
&& (annotation.coordinate.longitude == theCoordinate1.longitude)) {
但是,我不建议比较浮点数,即使它有时“有效”。如果必须比较坐标,请使用CLLocation的distanceFromLocation:
方法,看看两者之间的距离是否低于某个阈值,如10.0米。
检查注释是否是您正在查找的注释的另一种方法是保留对注释本身的引用(您传递给addAnnotation:
方法的注释),然后您可以执行{{1} }。
如果您不想保留对注释的引用,您还可以检查注释的标题是否是您要查找的标题(if (annotation == theAnnotation1)
)。
最好的方法是将自定义属性(理想情况下为int)添加到自定义注记类,并在viewForAnnotation中检查它。
一些其他无关的事情:
if ([annotation.title isEqualToString:@"Jeff"])
委托方法中处理按钮,而不是执行addTarget,该方法将提供对注释的引用(例如,请参阅How to find which annotation send showDetails?)。calloutAccessoryControlTapped
来重复使用视图(例如,请参阅EXC_BAD_ACCESS with MKPinAnnotationView)。