iPhone MapKit问题:viewForAnnotation不一致地设置pinColor吗? [英] iPhone MapKit problems: viewForAnnotation inconsistently setting pinColor?

查看:72
本文介绍了iPhone MapKit问题:viewForAnnotation不一致地设置pinColor吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个地图,该地图根据所讨论位置的类型/类别显示不同的图钉颜色.我知道这是很常见的事情,但是我很难让viewForAnnotation委托一致地更新/设置图钉颜色.
我有一个showThisLocation函数,该函数基本上循环遍历AddressAnnotations的列表,然后根据注释类(公交车站,医院等)设置

I'm trying setup a map that displays different pin colors depending on the type/class of the location in question. I know this is a pretty common thing to do, but I'm having trouble getting the viewForAnnotation delegate to consistently update/set the pin color.
I have a showThisLocation function that basically cycles through a list of AddressAnnotations and then based on the annotation class (bus stop, hospital, etc.) I set an


if( myClass == 1){
 [defaults setObject:@"1" forKey:@"currPinColor"];
 [defaults synchronize];
 NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
} else if( myClass ==2 ){
 [defaults setObject:@"2" forKey:@"currPinColor"];
 [defaults synchronize];
 NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
}
[_mapView addAnnotation:myCurrentAnnotation];

然后我的viewForAnnotation委托看起来像这样,

then my viewForAnnotation delegate looks like this,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if( annotation == mapView.userLocation ){ return nil; }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    MKPinAnnotationView *annView = nil;
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
    if( annView == nil ){
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    }

    annView.pinColor =  [defaults integerForKey:@"currPinColor"];
    NSLog(@"Pin color: %d", [defaults integerForKey:@"currPinColor"]);
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

问题是,尽管"if"块中的NSLog语句总是确认设置了颜色,但委托有时但并非总是以正确的颜色结尾.我还注意到,通常会发生的情况是,首次搜索新位置会将所有图钉设置为"if"块中的最后一种颜色,但是再次搜索相同位置会将图钉设置为正确的颜色.

The problem is that, although the NSLog statements in the "if" block always confirm that the color has been set, the delegate sometimes but not always ends up with the correct color. I've also noticed that what generally happens is that the first search for a new location will set all pins to the last color in the "if" block, but search for the same location again will set the pins to the correct color.

我怀疑我不应该以这种方式使用NSUserDefaults,但是我还尝试为MKAnnotation创建自己的子类,其中包括一个附加属性"currentPinColor",而当我这样做时,这允许我设置"currentPinColor"试图从委托方法访问"currentPinColor",编译器抱怨它对与MKAnnotation关联的"currentPinColor一无所知".我猜很公平,但是后来我尝试修改了委托方法,

I suspect I am not supposed to usen NSUserDefaults in this way, but I also tried to create my own subclass for MKAnnotation which included an additional property "currentPinColor", and while this allowed me to set the "currentPinColor", when I tried to access the "currentPinColor from the delegate method, the compiler complained that it didn't know anything about "currentPinColor in connection with MKAnnotation. Fair enough I guess, but then I tried to revise the delegate method,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MyCustomMKAnnotation>)annotation 

代替默认值


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 

这时,编译器抱怨说,在此委托上下文中,它对MyCustomMKAnnotation的协议一无所知.

at which point the compiler complained that it didn't know anything about the protocol for MyCustomMKAnnotation in this delegate context.

设置委托方法和/或MyCustomMKAnnotation的正确方法是什么,或者实现一致的pinColor设置的合适方法是什么.我只是想在这里尝试一些东西而已.

What is the proper way to set the delegate method and/or MyCustomMKAnnotation, or what is the appropriate way to achieve consistent pinColor settings. I'm just about out of ideas for things to try here.

推荐答案

我解决了自己的问题.有时只需提出问题就足够了! 问题是我尝试访问我的MKAnnotation委托的方式.

I solved my own problem. Sometimes just formulating the question is enough! The issue was the way I was trying to access my MKAnnotation delegate.

发布后,我碰巧遇到了这两个相关的帖子,

After posting I happened on these two related posts,

MKPinAnnotationView:是否可以使用三种以上的颜色?

查找注释当前位置的Mapkit问题

Cannonade用户对第一篇文章的回答提示

The answer to the first post, by user Cannonade, suggests


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{   
    // ... get the annotation delegate and allocate the MKAnnotationView (annView)
    if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame)
    {
        UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [annView addSubview:imageView];
    }
    // ...

但是我的问题是我不知道如何获得代表,并且它漂浮在上述片段的...省略号部分的某个地方.同样来自用户Cannonade的第二篇文章的答案也填补了这一缺失之处,

however my issue was that I didn't know how to the get the delegate and it was floating around somewhere in the ... ellipsis part of the above snippet. The answer to the second post, also by user Cannonade, filled this missing bit in,


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    AnnotationDelegate * delegate = (AnnotationDelegate*)view.annotation;
    // do stuff with delegate.position;
}

将这两个代码片段放在一起可以达到预期的效果,并最终解决了我的问题.

Putting these two snippets together produced the desired result and finally solved my problem.

然后工作的viewForAnnotation委托方法如下所示,

The working viewForAnnotation delegate method then looks like,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if( annotation == mapView.userLocation ){ return nil; }
    AddressAnnotation *delegate = annotation;  //THIS CAST WAS WHAT WAS MISSING!
    MKPinAnnotationView *annView = nil;
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
    if( annView == nil ){
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:delegate reuseIdentifier:@"currentloc"];
    }

    if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"green"] == NSOrderedSame) {
        annView.pinColor = MKPinAnnotationColorGreen;
    }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"purple"] == NSOrderedSame){
        annView.pinColor = MKPinAnnotationColorPurple;
    }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"red"] == NSOrderedSame){
        annView.pinColor = MKPinAnnotationColorRed;
    }
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

希望这会对其他人有所帮助.

Hopefully this will be of help to someone else.

谢谢大炮!

这篇关于iPhone MapKit问题:viewForAnnotation不一致地设置pinColor吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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