在我的地图视图中,我在json的位置加载。 (效果很好)
在我的json中,我有一个名为' type'这有助于在viewForAnnotation中设置图像(效果很好)
我有一组单选按钮,删除所有注释,然后根据类型重新加载某些注释(效果很好 - sorta)
这个问题是按下单选按钮时所有数据都正确但图标全部重新排列(看起来是随机的)。这个标题,副标题,电话和类型都是正确的。
看起来按下按钮时,不会触发视图注释。至少没有东西登录该方法。但那仅仅是图标被设置。我迷失在这一个。
CODE
- (void)whichPins:(int)t {
for(id key in json) {
id value = [json objectForKey:key];
NSString *address = [value valueForKey:@"address"];
NSString *latitude = [value valueForKey:@"latitude"];
NSString *longitude = [value valueForKey:@"longitude"];
NSArray* foo = [address componentsSeparatedByString: @":"];
NSString* address2 = [foo objectAtIndex: 0];
phone = [foo objectAtIndex: 1];
double myLatitude = [latitude doubleValue];
double myLongitude = [longitude doubleValue];
MKCoordinateRegion location1;
location1.center.latitude = myLatitude;
location1.center.longitude = myLongitude;
location1.span.longitudeDelta = 0.1;
location1.span.latitudeDelta = 0.1;
ann1 = [[[MapAnnotation alloc] init] autorelease];
ann1.title = [NSString stringWithFormat:@"%@",[value valueForKey:@"title"]];
ann1.subtitle = [NSString stringWithFormat:@"%@",address2];
ann1.phone = [NSString stringWithFormat:@"%@",phone];
ann1.coordinate = location1.center;
ann1.type = [value valueForKey:@"type"];
if (t == 0) {
[mapView addAnnotation:ann1];
} else if (t == 1 && [ann1.type isEqualToString:@"1"]) {
[mapView addAnnotation:ann1];
} else if (t == 2 && [ann1.type isEqualToString:@"2"]) {
[mapView addAnnotation:ann1];
} else if (t == 3) {
if ([ann1.type isEqualToString:@"0"] || [ann1.type isEqualToString:@"3"] || [ann1.type isEqualToString:@"4"]) {
[mapView addAnnotation:ann1];
}
} else {}
//NSLog(@"Title: %@ , type: %@", ann1.subtitle, ann1.type);
[phone retain];
[json retain];
}
//NSLog(@"/////////////////////////////////////////////////////////////////");
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else {
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
annotationView.draggable = NO;
if (![((MapAnnotation *)annotation).phone isEqualToString: @"0"]) {
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setImage:[UIImage imageNamed:@"map_phone_icon"] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
}
if ([((MapAnnotation *)annotation).type isEqualToString: @"0"]) {
annotationView.image = [UIImage imageNamed:@"popomap.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"1"]) {
annotationView.image = [UIImage imageNamed:@"robbery.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"2"]) {
annotationView.image = [UIImage imageNamed:@"other.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"3"]) {
annotationView.image = [UIImage imageNamed:@"firemap.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"4"]) {
annotationView.image = [UIImage imageNamed:@"doctormap.png"];
}
return annotationView;
}
return nil;
}
- (IBAction)onRadioBtn:(RadioButton*)sender {
[mapView removeAnnotations:mapView.annotations];
if ([sender.titleLabel.text isEqualToString:@"All"]) {
rbtnType = 0;
}
if ([sender.titleLabel.text isEqualToString:@"Crime"]) {
rbtnType = 1;
}
if ([sender.titleLabel.text isEqualToString:@"Other"]) {
rbtnType = 2;
}
if ([sender.titleLabel.text isEqualToString:@"Stations"]) {
rbtnType = 3;
}
[self whichPins:rbtnType];
}
答案 0 :(得分:0)
annotationView已经创建,所以当我再次加载引脚时,它忽略了图标代码:
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
将其更改为:
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView) {
if (![((MapAnnotation *)annotation).phone isEqualToString: @"0"]) {
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setImage:[UIImage imageNamed:@"map_phone_icon"] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
}
if ([((MapAnnotation *)annotation).type isEqualToString: @"0"]) {
annotationView.image = [UIImage imageNamed:@"popomap.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"1"]) {
annotationView.image = [UIImage imageNamed:@"robbery.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"2"]) {
annotationView.image = [UIImage imageNamed:@"other.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"3"]) {
annotationView.image = [UIImage imageNamed:@"firemap.png"];
} else if ([((MapAnnotation *)annotation).type isEqualToString: @"4"]) {
annotationView.image = [UIImage imageNamed:@"doctormap.png"];
}
return annotationView;