根据所选的MKAnnotationView动态更改leftCalloutAccessoryView [英] dynamically change leftCalloutAccessoryView based on the MKAnnotationView that is selected
问题描述
我有一个图像数组,这些图像与我的地图上的每个Annotation
相关联.我可以将图像静态地添加到leftCalloutAccessoryView
,但是不确定如何使它动态化.我希望清楚我的要求.每个注解都有自己要显示的图像,但是我不确定如何通过以下方法引用该图像;
I have an array of images, that are associated with each Annotation
on my map. I can statically add an image to the leftCalloutAccessoryView
but I am unsure how to make this dynamic. I hope its clear what I am asking. Each annotation has its own individual image that I want to display but I am unsure of how to reference the image in the following method;
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"PinViewAnnotation";
MyAnnotationView *pinView = (MyAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
pinView.canShowCallout = YES;
UIImageView *houseIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon"]];//static image
[houseIconView setFrame:CGRectMake(0, 0, 30, 30)];
pinView.leftCalloutAccessoryView = houseIconView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
我的数组"self.sandwiches"包含Sandwich
个对象,这些对象具有名称(NSString
)和imageName('NSString').
My array "self.sandwiches" contains Sandwich
objects that have a name (NSString
) and an imageName ('NSString').
我正在寻找一种解决方案,可以获取所选引脚的索引,类似于UITableView
,您可以获取其索引,然后使用indexPath.row
从数组访问它.
Im looking for a solution where I can get the index of the pin that is selected, similar to a UITableView
where you can get its index, and access it from the array using indexPath.row
.
我的注释类; .H #进口 #进口 #import
My Annotation class; .h #import #import #import
@interface SandwichAnnotation : NSObject<MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString * title;
@property (nonatomic,copy) NSString * subtitle;
@end
.m
#import "SandwichAnnotation.h"
@implementation SandwichAnnotation
@synthesize coordinate,title,subtitle;
@end
推荐答案
在viewForAnnotation
中,我不是获取引脚的索引"(这可以工作,但效率不如UITableView
),我建议将所需的数据添加到注释类本身.
In viewForAnnotation
, rather than "getting the index of the pin" (which would work but is less efficient here than with a UITableView
), I suggest adding the data required to the annotation class itself.
这样,数据变得更加独立,并且委托方法或其他地方的代码无需担心,知道或与注释对象的位置或结构的类型保持同步存储.只要您有对注释对象的引用,您将立即拥有该注释所需的所有数据(或者至少它内部将包含对相关数据的引用).
This way, the data is more self-contained and the code in the delegate method or elsewhere doesn't need to worry, know, or be kept in sync with where or what kind of structure the annotation object is stored in. As long as you have a reference to the annotation object, you will immediately have all the data needed for that annotation (or at least it will contain references to the related data within itself).
viewForAnnotation
委托方法提供对其需要查看的注释对象的引用(annotation
参数).通常将其键入为id<MKAnnotation>
,但是实际上它是所创建的确切类型的实例(您是SandwichAnnotation
还是地图视图是MKUserLocation
).
The viewForAnnotation
delegate method provides a reference to the annotation object it needs a view for (the annotation
parameter). It's typed generically as id<MKAnnotation>
but it is actually an instance of the exact type that was created (either SandwichAnnotation
by you or MKUserLocation
by the map view).
一种选择是使父Sandwich
类本身实现MKAnnotation
并消除SandwichAnnotation
类.这样,根本不需要搜索或引用,因为annotation
参数实际上是 一个Sandwich
.
One option is to make the parent Sandwich
class itself implement MKAnnotation
and eliminate the SandwichAnnotation
class. This way, no searching or references are needed at all since the annotation
parameter will actually be a Sandwich
.
但是,您可能想为注释对象保留一个单独的类(很好).在这种情况下,您可以在注释类中添加对父对象的引用.示例:
However, you may want to keep a separate class for your annotation objects (which is fine). In this case, you can add a reference to the parent object(s) in the annotation class. Example:
@interface SandwichAnnotation : NSObject<MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString * title;
@property (nonatomic,copy) NSString * subtitle;
@property (nonatomic,retain) Sandwich * whichSandwich; // <-- add reference
@end
创建SandwichAnnotation
时,设置注释用于Sandwich
的引用:
When creating a SandwichAnnotation
, set the reference to which Sandwich
the annotation is for:
for (Sandwich *currentSandwich in self.sandwiches) {
SandwichAnnotation *sa = [[SandwichAnnotation alloc] init...];
sa.coordinate = ...
sa.title = ...
sa.whichSandwich = currentSandwich; // <-- set reference
[mapView addAnnotation:sa];
}
最后,在viewForAnnotation
中,如果annotation
的类型为SandwichAnnotation
,则设置leftCalloutAccessoryView
:
Finally, in viewForAnnotation
, if annotation
is of type SandwichAnnotation
, set the leftCalloutAccessoryView
:
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if (! [annotation isKindOfClass:[SandwichAnnotation class]]) {
//If annotation is not a SandwichAnnotation, return default view...
//This includes MKUserLocation.
return nil;
}
//At this point, we know annotation is of type SandwichAnnotation.
//Cast it to that type so we can get at the custom properties.
SandwichAnnotation *sa = (SandwichAnnotation *)annotation;
NSString *annotationIdentifier = @"PinViewAnnotation";
MyAnnotationView *pinView = (MyAnnotationView *) [mv dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
pinView.canShowCallout = YES;
//Here, just initialize a blank UIImageView ready to use.
//Set image below AFTER we have a dequeued or new view ready.
UIImageView *houseIconView = [[UIImageView alloc] init];
[houseIconView setFrame:CGRectMake(0, 0, 30, 30)];
pinView.leftCalloutAccessoryView = houseIconView;
}
else
{
pinView.annotation = annotation;
}
//At this point, we have a dequeued or new view ready to use
//and pointing to the correct annotation.
//Update image on the leftCalloutAccessoryView here
//(not just when creating the view otherwise an annotation
//that gets a dequeued view will show an image of another annotation).
UIImageView *houseIconView = (UIImageView *)pinView.leftCalloutAccessoryView;
NSString *saImageName = sa.whichSandwich.imageName;
UIImage *houseIcon = [UIImage imageNamed: saImageName];
if (houseIcon == nil) {
//In case the image was not found,
//set houseIcon to some default image.
houseIcon = someDefaultImage;
}
houseIconView.image = houseIcon;
return pinView;
}
这篇关于根据所选的MKAnnotationView动态更改leftCalloutAccessoryView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!