每个针脚都有不同的自定义图片 [英] Different custom image for each pin
问题描述
这听起来像是一个通用的问题,但是我只想为所有图钉使用相同的图像找到答案,而我却没有.
This may sound like a generic question but I've only found answers if I want the same image for all my pins, which i don't.
这就是我现在的工作方式:
That's how i'm working right now :
我所有的位置都存储在一个位置数组中(带有长,纬度,名称,引脚名称的自定义类).
I have all my locations in an array of Locations (custom class with long, lat, name, pin name).
在viewdidLoad中,我循环该数组并为找到的每个对象创建引脚,请参见以下代码:
in the viewdidLoad I loop that array and create my pins with every object found, see following code :
for(int i = 0 ; i<[_locationList count] ; i++)
{
Location *item = [_locationList objectAtIndex:i];
CLLocationCoordinate2D coordinate;
coordinate.latitude = item.locationLatitude;
coordinate.longitude = item.locationLongitude;
NSString *title = item.locationName;
CustomAnnotation* ann = [CustomAnnotation new];
ann.name = title;
ann.coordinate = coordinate;
ann.pinName = [NSString stringWithFormat:@"pin%i.png",item.idPin];
[self.map addAnnotation:ann];
}
这很简单,部分来自CustomAnnotation类,它是以下代码:
This is pretty straight forward, part from the CustomAnnotation class, which is the following code :
@interface CustomAnnotation : MKPointAnnotation <MKAnnotation>{
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *pinName;
@end
这全都是我在互联网上看到的东西,我有点相信到那时为止,这都是正确的.
This is all from stuff i've seen around the internet, and I kinda believe it's all correct up to that point.
在我看来,我仍在创建非常经典的图钉,它们仅具有一个属性(pinName),这就是为什么它来自自定义类的原因.
In my mind, i'm still creating very classic pins, they just have one more property (pinName), which is why it's coming from the custom class.
现在,在viewForAnnotation中,我绝对不知道如何告诉它获取该pinName并使用它.
Now, in the viewForAnnotation, i have absolutly NO IDEA how to tell it to get that pinName and use it.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = // I wanted to write annotation.pinName But it's just not that.
pinView.calloutOffset = CGPointMake(0, 32);
}else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
我想念什么?我显然做错了事,但我无法弄清楚,我仍然对MKAnnotationsView&的区别感到困惑. MKPointAnnotation& MKPinAnnotation,...
What am I missing? I'm obviously doing something wrong but i jsut can't figure it out, and i'm still quite confused with the differences between MKAnnotationsView & MKPointAnnotation & MKPinAnnotation, ...
更多信息:引脚名称是"pinX.png",X是1到12之间的数字.我只想使用该名称,以便程序可以在图片所在的资源中找到它.
More info : the pin names are " pinX.png ", X being a number between 1 and 12. I just want to use that name so the program can find it in the ressources where the picture lies.
推荐答案
由于注释的类型为CustomAnnotation
,因此检查注释的类型是否为MKPointAnnotation
而不是MKPointAnnotation
更为准确.
Since your annotations are of type CustomAnnotation
, it would be more accurate to check if the annotations are of that kind instead of MKPointAnnotation
.
然后,将annotation
参数转换为自定义类将使您可以轻松地在其中访问自定义属性,如下所示:
Then, casting the annotation
parameter to your custom class will let you easily access the custom properties in it like this:
CustomAnnotation *ca = (CustomAnnotation *)annotation;
pinView.image = [UIImage imageNamed:ca.pinName];
但是,由于每个注解的图像可能不同,因此应在创建注解视图或后将其设置为 (不仅在if (!pinView)
阻止-否则注释可能最终会重新使用不再可见的另一个注释中的视图,并显示错误的图片.
However, because the image can be different for each annotation, you should set it after the annotation view has been created or dequeued (not only in the if (!pinView)
block -- otherwise an annotation could end up re-using the view from another annotation no longer visible and the wrong image will show).
更新后的方法如下:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[CustomAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.calloutOffset = CGPointMake(0, 32);
//NOTE:
//If the calloutOffset needs to be different for each image,
//then this line should also be set below with the image.
}
else {
pinView.annotation = annotation;
}
//Set image on view AFTER we have a new or dequeued view
//because image is based on each annotation...
CustomAnnotation *ca = (CustomAnnotation *)annotation;
pinView.image = [UIImage imageNamed:ca.pinName];
//Might want to check that the UIImage is not nil
//in case pinName is invalid since that would result in
//an invisible annotation view. If the UIImage is nil,
//set pinView.image to some default image.
return pinView;
}
return nil;
}
有关MapKit类之间差异的困惑,请参见:
For the confusion about the differences between the MapKit classes, see:
-
我应该使用MKAnnotation,MKAnnotationView还是MKPinAnnotation?.即使该问题被标记为MonoTouch,它仍然适用.
Should I use MKAnnotation, MKAnnotationView or MKPinAnnotation?. Even though that question is tagged MonoTouch, it still applies.
MKMapView,animateDrop?可能也有帮助.
这篇关于每个针脚都有不同的自定义图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!