【问题标题】:MapView Problems - Pin change color when map relodedMapView 问题 - 地图重新加载时引脚更改颜色
【发布时间】:2013-03-27 11:52:09
【问题描述】:

刷新完成后,我的 PIn 颜色 mapView 出现问题。 在我的应用程序中,我用两种颜色显示一些点,以确定服务是否可用。 在第一次启动时,没有出现任何问题。代码如下:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    [self dowloadPoint];    // here I exucte the first start 

}

- (void)dowloadPoint{
    NSURL *url1 =[NSURL URLWithString:@"http:MYUSRL"];
    NSData *datos1 =[[NSData alloc] initWithContentsOfURL:url1];

        [self plotBarPosition:datos_string1];     //Here I call the plotBarPosition method
}


- (void)plotBarPosition:(NSString *)datos_string1 {

    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }

    // Parse the string into JSON
    NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"];

    // Get the objects you want, e.g. output the second item's client id
    NSArray *items_properties = [json valueForKeyPath:@"properties"];
    NSArray *items_geo = [json valueForKeyPath:@"geometry"];

        for (int i = 0; i < [json count]; i++){

            NSString *nomprePunto =[[items_properties objectAtIndex:i] objectForKey:@"title"]; 

            NSNumber *lat =[[[items_geo objectAtIndex:i] objectForKey:@"coordinates"] objectAtIndex:0];


            NSNumber *lon =[[[items_geo objectAtIndex:i] objectForKey:@"coordinates"] objectAtIndex:1];

            CLLocationCoordinate2D coordinate;
            coordinate.latitude = lat.doubleValue;
            coordinate.longitude = lon.doubleValue;

            //ESTADO
            NSString *description = [[items_properties objectAtIndex:i] objectForKey:@"description"];
            NSString *estado_punto = [[NSString alloc]init];

            if ([description rangeOfString:@"Averiado"].location == NSNotFound) {
                estado_punto = @"Available";
            } else {
                estado_punto = @"NOt Available";
                averiados ++;
             }
            NSString *averiadosStr = [NSString stringWithFormat:@"%d",averiados];
           averiadosLabel.text = averiadosStr;

            MyLocation *location =[[MyLocation alloc] initWithName:nomprePunto coordinate:coordinate estado:estado_punto];

            [_mapView addAnnotation:location];
    }


}



- (MKPinAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyLocation *)annotation {

        static NSString *identifier = @"MyLocation";
        if ([annotation isKindOfClass:[MyLocation class]]) {
            MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

            if (annotationView == nil) {
                annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

                annotationView.enabled = YES;
                annotationView.canShowCallout = YES;

                if([[annotation estado] isEqualToString:@"En Servicio"])
                annotationView.pinColor = MKPinAnnotationColorGreen;

                } else {
                annotationView.annotation = annotation;
            }

            return annotationView;
        }

        return nil;
    }

但是当我添加一个作为函数的 refres 按钮时,它只是一次刷新调用 dowloadPoint,

- (IBAction)refresh{

    [self dowloadPoint];
}

引脚的颜色以“随机方式”变化,与点的真实状态不对应。 关于正在发生的事情有什么想法吗?提前致谢。

编辑:这似乎是由于:

for (id<MKAnnotation> annotation in _mapView.annotations) {
    [_mapView removeAnnotation:annotation];
}

删除它,应用程序可以正常工作,但引脚区域淹没在以前的区域...:S

【问题讨论】:

    标签: xcode mkmapview mkpinannotationview


    【解决方案1】:

    图钉的默认颜色是红色。如果MyLocation 对象的estado 属性等于@"En Servicio",则将其设置为绿色。我知道,当您的estado 属性等于@"En Servicio" 时,有时颜色是红色,或者有时不是绿色。
    一个原因可能是当您按下刷新按钮时,您的 MyLocation 对象根本不再存在。在这种情况下,您可能仍然有一个指向它曾经存在的内存位置的指针,但该位置可能已被任何内容覆盖,从而导致随机颜色。
    这可能发生,例如如果您的 MyLocation 对象已创建为自动释放对象,该对象在您返回主事件循环时已释放,即处理用户交互。
    如果您使用的是 ARC,则不应出现这种情况。

    【讨论】:

    • 嗨。感谢您的回答。我正在使用 ARC,所以问题不取决于版本,是吗?
    • 嗨,我只有一个想法:MKAnnotationView 类参考的文档对其注释属性说:“您不应该直接更改此属性的值。”但是你在声明annotationView.annotation = annotation; 中做到这一点。也许这会导致问题?
    • 问题似乎是由于使用了:for (id annotation in _mapView.annotations) { [_mapView removeAnnotation:annotation];如果我删除它可以正常工作,但大头针会淹没在以前的大头针之上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多