gpt4 book ai didi

ios - 在 xcode 中将不同的图像添加到不同的注释 View

转载 作者:行者123 更新时间:2023-11-29 11:22:37 24 4
gpt4 key购买 nike

我正在尝试将不同的图像添加到不同的注释 View ,换句话说,我想要一个独特的图片对应于每个独特的图钉。这是我正在尝试的:

- (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 类型的变量。您无法比较两者。

您要做的是将请求 View 的当前注释的坐标与 theCoordinate1 中的坐标进行比较。为此,如果必须的话,您需要这样的东西:

if ((annotation.coordinate.latitude == theCoordinate1.latitude) 
&& (annotation.coordinate.longitude == theCoordinate1.longitude)) {

但是,我不建议以这种方式比较 float ,即使它有时“有效”。如果您必须比较坐标,请使用 CLLocation 的 distanceFromLocation: 方法并查看两者之间的距离是否低于某个阈值,例如 10.0 米。

另一种检查注解是否是您要查找的注解的方法是保留对注解本身的引用(您传递给 addAnnotation: 方法的那个),然后您可以if (annotation == theAnnotation1).

如果您不想保留对注释的引用,您还可以检查注释的标题是否是您要查找的标题 (if ([annotation.title isEqualToString:@"Jeff "])).

最好的方法是将自定义属性(最好是 int)添加到自定义注释类并在 viewForAnnotation 中检查它。


其他一些不相关的事情:

  • 不执行 addTarget,而是在 map View 自己的 calloutAccessoryControlTapped 委托(delegate)方法中处理按钮按下,这将提供对注释的引用(例如,请参见 How to find which annotation send showDetails?)。
  • 您有一个要“出队”的评论,但您没有这样做。建议在 viewForAnnotation 中使用 dequeueReusableAnnotationViewWithIdentifier 来重用 View (例如参见 EXC_BAD_ACCESS with MKPinAnnotationView)。

关于ios - 在 xcode 中将不同的图像添加到不同的注释 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6146984/

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