Xcode 地图视图问题-在地图重新编码时锁定更改颜色
刷新完成后,PIn颜色贴图视图出现问题。 在我的I应用程序中,我用两种颜色显示某个点,以确定服务是否可用。 在第一次启动时,不会出现任何问题。代码是以下代码:Xcode 地图视图问题-在地图重新编码时锁定更改颜色,xcode,mkmapview,mkpinannotationview,Xcode,Mkmapview,Mkpinannotationview,刷新完成后,PIn颜色贴图视图出现问题。 在我的I应用程序中,我用两种颜色显示某个点,以确定服务是否可用。 在第一次启动时,不会出现任何问题。代码是以下代码: - (void)viewDidLoad { [super viewDidLoad]; [self dowloadPoint]; // here I exucte the first start } - (void)dowloadPoint{ NSURL *url1 =[NSURL URLWith
- (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;
}
引脚的颜色以“随机方式”变化,与点的真实状态不一致。
对正在发生的事情有什么想法吗?提前谢谢
编辑:问题的原因是:
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
for(映射视图中的id注释.annotations){
[_MapViewRemoveAnnotation:annotation];
}
擦除它,应用程序可以正常工作,但pin区域淹没在先前的区域中…:Spin的默认颜色为红色。如果
MyLocation
对象的estado
属性等于@“En Servicio”
,则将其设置为绿色。我知道,当您的estado
属性等于@“En Servicio”
时,有时颜色是红色,当属性不等于@“En Servicio”
时,有时颜色是绿色。一个原因可能是当您按下刷新按钮时,
MyLocation
对象不再存在。在这种情况下,您可能仍然有一个指向它曾经存在的内存位置的指针,但该位置可能已被任何内容覆盖,从而导致随机颜色。例如,如果您的
MyLocation
对象已创建为自动释放对象,并且在返回到主事件循环时已释放,即处理用户交互,则可能发生这种情况。如果使用ARC,则不应出现这种情况。Hi。谢谢你的回答。我使用的是ARC,所以问题不依赖于发行版,是吗?嗨,我还有一个想法:MKAnnotationView类引用的文档对其注释属性说:“不应该直接更改此属性的值。”但您可以在语句annotationView.annotation=annotation;中执行此操作;。可能是这导致了问题?问题似乎是由于使用了:for(id annotation in _mapView.annotations){[\u mapView removeanotation:annotation];}如果我删除它可以正常工作,但是管脚会淹没在以前的管脚之上。
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}