如何显示通过plist加载的多个自定义注释(图钉和左图标)? [英] How do I show multiple custom annotations (pin & left icon) loaded via plist?

查看:89
本文介绍了如何显示通过plist加载的多个自定义注释(图钉和左图标)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个代码在工作时显示自定义引脚和徽标还是只有1个引脚带有自定义徽标?请参阅下面的plist,只有错误是mapView的本地声明隐藏了实例变量。

will this code when working show custom pins and logos or just 1 pin with custom logos? please see plist below, only error is Local declaration of mapView hides instance variable.

<plist version="1.0">
<array>
<dict>
    <key>stationIdKey</key>
    <string>BP</string>
    <key>stationNameKey</key>
    <string>Atkinson Dam Cabin Village</string>
    <key>stationAddressKey</key>
    <string>Atkinson Dam Road, Atkinson Dam</string>
    <key>stationLatitudeKey</key>
    <string>-27.415056</string>
    <key>stationLongitudeKey</key>
    <string>152.43057</string>
</dict>
<dict>
    <key>stationIdKey</key>
    <string>Shell</string>
    <key>stationNameKey</key>
    <string>BP - AYR DCA</string>
    <key>stationAddressKey</key>
    <string>108 Edwards Street, AYR</string>
    <key>stationLatitudeKey</key>
    <string>-19.57094107</string>
    <key>stationLongitudeKey</key>
    <string>147.4025662</string>
</dict>

和mapViewController.m文件

and the mapViewController.m file

- (void)viewDidload
{

    [super viewDidLoad];

NSString *path = [[NSBundle mainBundle] pathForResource:@"stations" ofType:@"plist"];
NSArray *anns = [NSArray arrayWithContentsOfFile:path];
NSLog(@"anns=%@",anns);

for(NSMutableDictionary *note in anns) {
    float realLatitude = [[note objectForKey:@"stationLatitudeKey"] floatValue];
    float realLongitude = [[note objectForKey:@"stationLongitudeKey"] floatValue];        
    MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = [note objectForKey:@"stationNameKey"];
    myAnnotation.subtitle = [note objectForKey:@"stationAddressKey"];
    myAnnotation.stationIdKey = [note objectForKey:@"stationIdKey"];
    [mapView setDelegate:self];
    [mapView addAnnotation:myAnnotation];
    [myAnnotation release];        
} }



-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{  if ([annotation isKindOfClass:[MyAnnotation class]])
{
    static NSString *reuseId = @"customAnn";

    MKAnnotationView *customAnnotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (customAnnotationView == nil)
    {
        customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
        UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"];
        [customAnnotationView setImage:pinImage];
        customAnnotationView.canShowCallout = YES;
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        customAnnotationView.rightCalloutAccessoryView = rightButton;
    }

    NSString *iconFilename = @"";
    MyAnnotation *myAnn = (MyAnnotation *)annotation;
    if ([myAnn.stationIdKey isEqualToString:@"BP"])
        iconFilename = @"bp-logo.png";
    else
        if ([myAnn.stationIdKey isEqualToString:@"Caltex"])
            iconFilename = @"caltex.png";
        else
            if ([myAnn.stationIdKey isEqualToString:@"Shell"])
                iconFilename = @"shell.png";
    UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease];
    customAnnotationView.leftCalloutAccessoryView = leftIconView;

    customAnnotationView.annotation = annotation;

    return customAnnotationView; 
}

return nil; }


-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{ if ([view.annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *myAnn = (MyAnnotation *)view.annotation;
    NSLog(@"callout button tapped for station id %@", myAnn.stationIdKey);
}
else
{
    NSLog(@"callout button tapped for annotation %@", view.annotation);
} }


推荐答案

(发布)对于稍微修改过的问题的单独答案。最初的问题是如何根据注释对象的某些属性更改注释标注图像。修改后的问题是为什么注释根据最新代码并没有出现以及如何修复viewForAnnotation中的警告。)

问题中最新代码唯一明显的问题是 viewDidLoad 方法拼写错误,这会阻止该代码被调用。请更改此:

The only obvious problem with the latest code in the question is that the viewDidLoad method is spelled wrong which prevents that code from getting called at all. Please change this:

- (void)viewDidload  //the lowercase "l" is not correct, must be uppercase

到此:

- (void)viewDidLoad



还有其他一些要检查的东西以防万一:


A few other things to check just in case:


  • Plist文件应该真的像<?xml ...><! DOCTYPE ...>< plist ...>< array>< dict> xxx< / dict>< dict> yyy< / dict>< dict> zzz< / dict>< / array> < / plist>

  • 确保像 pin-green.png 等图片文件有已被添加到项目中并以这种方式命名完全(设备上的大写/小写事项)

  • Plist file should really be like <?xml ...><!DOCTYPE ...><plist ...><array><dict>xxx</dict><dict>yyy</dict><dict>zzz</dict></array></plist>
  • Make sure the image files like pin-green.png, etc have been added to the project and are named exactly that way (uppercase/lowercase matters on the device)

接下来,要修复 viewForAnnotation 中的编译器警告,请将 mapView 参数的名称更改为其他名称,以使其与视图控制器中的 mapView ivar。以下示例将其更改为 aMapView (两个标有 ^^^^^^ 的地方):

Next, to fix the compiler warning in viewForAnnotation, change the name of the mapView parameter to something else so it's different from the mapView ivar in the view controller. Following example changes it to aMapView (two places marked with ^^^^^^):

-(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation
                                          ^^^^^^^^
{  
    if ([annotation isKindOfClass:[MyAnnotation class]])
    {
        static NSString *reuseId = @"customAnn";

        MKAnnotationView *customAnnotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
                                                  ^^^^^^^^
        if (customAnnotationView == nil)
        {
            customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
            UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"];
            [customAnnotationView setImage:pinImage];
            customAnnotationView.canShowCallout = YES;
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            customAnnotationView.rightCalloutAccessoryView = rightButton;
        }

        NSString *iconFilename = @"";
        MyAnnotation *myAnn = (MyAnnotation *)annotation;
        if ([myAnn.stationIdKey isEqualToString:@"BP"])
            iconFilename = @"bp-logo.png";
        else
            if ([myAnn.stationIdKey isEqualToString:@"Caltex"])
                iconFilename = @"caltex.png";
            else
                if ([myAnn.stationIdKey isEqualToString:@"Shell"])
                    iconFilename = @"shell.png";
        UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease];
        customAnnotationView.leftCalloutAccessoryView = leftIconView;

        customAnnotationView.annotation = annotation;

        return customAnnotationView; 
    }

    return nil; 
}



这是它的外观(你的引脚和标注图像可能不同):


This is how it looks (your pin and callout images may be different):

如果你想要引脚本身要有不同的图像而不是他们的标注,然后在 viewForAnnotation 中,调用 setImage 而不是设置 leftCalloutAccessoryView

If you want the pins themselves to have different images instead of their callouts, then in viewForAnnotation, call setImage instead of setting the leftCalloutAccessoryView.



另一个小的,不相关的,就是你不需要打电话给<$ c for-loop中的$ c> mapView setDelegate 。你只需要在for循环之前调用一次(如果你已经将地图视图的委托连接到xib中的File的Owner,你根本不需要在代码中执行它。)


Another minor, unrelated, thing is that you don't need to call mapView setDelegate inside the for-loop. You only need to call it once before the for-loop (and if you've already connected the map view's delegate to File's Owner in the xib, you don't need to do it in code at all).

这篇关于如何显示通过plist加载的多个自定义注释(图钉和左图标)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆