2012-12-02 18 views
1

我有一張加載了3種標準顏色的地圖的地圖。這些引腳根據解析並存儲到數組中的xml中的值進行着色。根據顏色隱藏或刪除地圖註釋

我想添加一個分段的控制,2個按鈕:

按鈕1,只顯示綠色的引腳。

和按鈕2僅顯示紅色和紫色的針腳。

我已閱讀有關爲每個引腳顏色添加3個不同的陣列並刪除引腳數組,但我想維護一個數組。如果可能,我會如何做到這一點。我知道如何實現分段控制,但我難以啓用或禁用它們。

繼承人我for循環:這將創建的針腳和分配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

最簡單的方法是最初和每當過濾條件的變化(例如,當分段控制的值的變化。):

  1. 刪除地圖上的所有註釋。使用removeAnnotations(或在某些循環中使用removeAnnotation)。
  2. 只添加通過過濾條件的那些。您通過locationArray循環,並且只能通過addAnnotation來通過該過濾器。

這可能會導致一些閃爍,因爲註釋被刪除並再次添加,但相對容易實現。


另一種替代方法是執行以下操作(最初和過濾器更改後)。遍歷locationArray和:

  • 如果該項目通過過濾器這是已經在地圖的annotations陣列,將其添加到地圖
  • 如果該項目沒有通過過濾器是地圖的annotations陣列中,從地圖

然而刪除它,這個方法要求你的註解對象中有一些獨特的屬性可以用來實現「已經在地圖中」存在檢查(不建議使用座標)。您可能需要將該獨特的屬性添加到MyAnnotation類。

或者,如果你不停的做自己的主人陣列中的所有可能MyAnnotation對象,你可以簡單地使用containsObject來測試,如果它是地圖的annotations陣列英寸

+0

再次無法再感謝你,另一個漫長的夜晚,簡單的事情似乎不會彈出。我建議您在循環中添加註釋,並在符合條件時僅添加註釋,即if([live isEqualToString:@「Active」])myAnnotation.pinColor = MKPinAnnotationColorGreen; [incidentsMap addAnnotation:myAnnotation]; //如果只想顯示綠色的引腳等等!當你擁有一雙清新的眼睛時,這很容易! } –