刷新完成后,我的 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