在xcode中将不同的图像添加到不同的注释视图 [英] Adding different images to different annotation views in xcode
问题描述
我正在尝试将不同的图像添加到不同的注释视图,换句话说,我想要一个唯一的图片对应于每个唯一的图钉.这是我正在尝试的:
I am trying to add different images to different annotation views, in other words, i want a unique pic to correspond to each unique pin. Here is what I am trying:
- (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) {
我不确定什么地方出了问题,也无法找出另一种方法来识别单个注释.任何帮助都将不胜感激!
I am not sure quite what is wrong, nor can i figure out another way to identify the individual annotation. Any help is greatly appreciated!!
推荐答案
该行给出了错误,因为CLLocationCoordinate2D
是struct的类型,而theCoordinate1
是类型为CLLocationCoordinate2D
的变量.您无法将两者进行比较.
That line gives an error because CLLocationCoordinate2D
is a type of struct and theCoordinate1
is, I assume, a variable of type CLLocationCoordinate2D
. You can't compare the two.
您要尝试做的是将请求其视图的当前注释的坐标与theCoordinate1
中的坐标进行比较.为此,如果需要,您需要这样的东西:
What you are trying to do is compare the coordinates of the current annotation for which a view is being requested to the coordinates in theCoordinate1
. To do that, if you must, you need something like this:
if ((annotation.coordinate.latitude == theCoordinate1.latitude)
&& (annotation.coordinate.longitude == theCoordinate1.longitude)) {
但是,即使有时起作用",我也不建议以这种方式比较浮点数.如果必须比较坐标,请使用CLLocation的distanceFromLocation:
方法,查看两者之间的距离是否低于某个阈值,例如10.0米.
However, I don't recommend comparing floating point numbers that way even if it "works" sometimes. If you must compare coordinates, use CLLocation's distanceFromLocation:
method and see if the distance between the two is below some threshold like 10.0 meters.
检查注释是否是您正在寻找的另一种方法是保留对注释本身的引用(传递给addAnnotation:
方法的注释),然后可以执行if (annotation == theAnnotation1)
.
Another way to check if the annotation is the one you're looking for is to keep a reference to the annotation itself (the one you passed to the addAnnotation:
method) and then you can do if (annotation == theAnnotation1)
.
如果您不想保留对注释的引用,也可以检查注释的标题是否是您要查找的标题(if ([annotation.title isEqualToString:@"Jeff"])
).
If you don't want to keep a reference to the annotations, you can also check if the title of the annotation is the one your're looking for (if ([annotation.title isEqualToString:@"Jeff"])
).
最好的方法是将自定义属性(最好是int)添加到自定义注释类,然后在viewForAnnotation中进行检查.
The best way is to add a custom property (ideally an int) to a custom annotation class and check for that in viewForAnnotation.
其他一些无关的东西:
A few other unrelated things:
- 代替执行addTarget,而是处理地图视图自己的
calloutAccessoryControlTapped
委托方法中的按钮按下操作,该方法将提供对注释的引用(请参阅dequeueReusableAnnotationViewWithIdentifier重用视图(请参阅 EXC_BAD_ACCESS与例如MKPinAnnotationView .
- Instead of doing addTarget, handle the button press in the map view's own
calloutAccessoryControlTapped
delegate method which will give a reference to the annotation (see How to find which annotation send showDetails? for example). - You have a comment to "dequeue" but you're not doing it. It's recommended to use
dequeueReusableAnnotationViewWithIdentifier
in viewForAnnotation to re-use views (see EXC_BAD_ACCESS with MKPinAnnotationView for example).
这篇关于在xcode中将不同的图像添加到不同的注释视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!