我的地图上装有3种标准颜色的针脚。这些引脚根据xml中的值进行着色,该值被解析并存储到数组中。
我想添加一个带有2个按钮的分段控件:
按钮1仅显示绿色针脚。
和按钮2仅显示红色和紫色针脚。
我已经阅读过为每种引脚颜色添加3个不同的数组并删除引脚数组但我想保留一个数组。如果可能的话,我该怎么办?我知道如何实现分段控件,但我不知道如何打开或关闭它们。
继承我的循环:创建引脚并分配3种颜色,工作正常。
//Count the array of annotations and add them dynamically to the map.
for (int i = 0; i < locationArray.count; i++) {
myAnnotation =[[MyAnnotation alloc] init];
NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];
type = [[locationArray objectAtIndex:i]xmlType];
imageId = [[locationArray objectAtIndex:i]xmlImageId];
address = [[locationArray objectAtIndex:i]xmlAddress];
email = [[locationArray objectAtIndex:i]xmlEmail];
phone = [[locationArray objectAtIndex:i]xmlPhone];
live = [[locationArray objectAtIndex:i]xmlLive];
form = [[locationArray objectAtIndex:i]xmlForm];
name = [[locationArray objectAtIndex:i]xmlName];
//Change the 0 to Active ticket and 1 to Closed 2 to False and 3 to Not Found and 4 to Other
if ([live isEqualToString:@"0"]) {
live = @"Active";
}
else if ([live isEqualToString:@"1"]){
live = @"Closed";
}
else if([live isEqualToString:@"2"]){
live = @"False";
}
else if ([live isEqualToString:@"3"]){
live = @"Not Found";
}
else if ([live isEqualToString:@"4"]){
live = @"Other";
}
double theLatitude = [latString doubleValue];
double theLongtitude = [lonString doubleValue];
userLocation.latitude=theLatitude;
userLocation.longitude=theLongtitude;
myAnnotation.coordinate=userLocation;
myAnnotation.title=[NSString stringWithFormat:@"%@", imageId];
myAnnotation.subtitle=[NSString stringWithFormat:@"%@", type];
//Setting pin colours here based on value from XML
if ([live isEqualToString:@"Active"]){
myAnnotation.pinColor = MKPinAnnotationColorGreen;
}else if ([live isEqualToString:@"Closed"]){
myAnnotation.pinColor = MKPinAnnotationColorRed;
}
else if ([live isEqualToString:@"Not Found"]){
myAnnotation.pinColor = MKPinAnnotationColorPurple;
}
[incidentsMap addAnnotation:myAnnotation];
}
并且继承了我的分段控制
-(IBAction)segmentedControl:(id)sender{
if (mapFilter.selectedSegmentIndex == 0) {
NSLog(@"Active");
}
else if (mapFilter.selectedSegmentIndex == 1){
NSLog(@"Closed");
//Remove Red and Purple Pins here from view when segmented control button button is touched.........................
}
else if (mapFilter.selectedSegmentIndex == 2){
UIAlertView *mapSelector = [[UIAlertView alloc]initWithTitle:@"Select Map Type" message:@"Choose from 3 map views" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Default", @"Hybrid", @"Satelite", nil];
[mapSelector show];
}
}
答案 0 :(得分:0)
最简单的方法是在最初和过滤标准发生变化时(例如,当分段控件的值发生变化时):
removeAnnotations
(或removeAnnotation
。locationArray
并仅为通过过滤器的人调用addAnnotation
。这可能会导致一些闪烁,因为注释会被删除并再次添加,但相对容易实现。
另一种方法是执行以下操作(最初和过滤器更改后)。循环遍历locationArray
和:
annotations
数组中已经不,则将其添加到地图annotations
数组中,则将其从地图中删除但是,此方法要求您在注释对象中具有一些唯一属性,可用于实现“已在地图中”存在检查(不建议使用坐标)。您可能需要将该唯一属性添加到MyAnnotation
类。
或者,如果您保留自己的所有可能MyAnnotation
个对象的主数组,则只需使用containsObject
来测试它是否在地图的{{1}中} array。