2016-08-16 3 views
0

実際には、マーカーにiconviewを追加して、そのマーカーをタップすると別のアイコンビューに変更します。これはdidTapMarkerデリゲートメソッドでiconviewを追加することで簡単に行うことができます。しかし、別のマーカーを選択したときにデフォルトの表示に変更する方法。ちょうどトグルボタンToggle ButtonのようにGoogle Makerを作成する

- (void)viewDidLoad 
{ 
    for(int i=0;i<lat.count;i++) 
    { 
     CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[lat objectAtIndex:i]doubleValue],[[longit objectAtIndex:i]doubleValue]); 
     GMSMarker *marker = [GMSMarker markerWithPosition:position];       
     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 69, 60)];    
     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 69, 21)];   
     label.text = @"Hello";    
     label.font = [UIFont systemFontOfSize:10];    
     label.textAlignment = NSTextAlignmentCenter;    
     label.textColor = [UIColor colorWithRed:24.0/255.0 green:59.0/255.0 blue:91.0/255.0 alpha:1.0];    
     label.backgroundColor = [UIColor colorWithRed:177.0/255.0 green:177.0/255.0 blue:177.0/255.0 alpha:1.0];    
     UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, 69, 38)];    
     [btn setImage:[UIImage imageNamed:@"map2_"] forState:UIControlStateNormal];       
     [view addSubview:label];    
     [view addSubview:btn];    
     marker.iconView = view; 
     marker.appearAnimation = kGMSMarkerAnimationPop;  
     marker.map = _mapView;    
    } 
} 

とあなたはuserDataそのためGMSMarkerのプロパティをすることができます

-(BOOL) mapView:(GMSMapView *) mapView didTapMarker:(GMSMarker *)marker 
{   
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 69, 60)];   
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 69, 21)];   
    label.text = @"Hello"; 
    label.font = [UIFont systemFontOfSize:10];   
    label.textAlignment = NSTextAlignmentCenter;   
    label.textColor = [UIColor whiteColor];   
    label.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:139.0/255.0 blue:58.0/255.0 alpha:1.0];   
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, 69, 38)];   
    [btn setImage:[UIImage imageNamed:@"map3_"] forState:UIControlStateNormal];     
    [view addSubview:label];   
    [view addSubview:btn];   
    marker.iconView = view;  
    return YES; 
} 
+0

のようなdidTapMarkerチェックにちょうど方法** didTapMarker **内部で一意にあなたのマーカーを同定し、それに応じてカスタムのものを行います。.. – vaibhav

答えて

1

didTapMarkerデリゲートメソッドのように、最初にすべてのあなたのマーカーでのuserDataを設定し、そのループのためにあなたの内側に、この行を追加します。 viewDidLoadMapにマーカーを追加しています。

marker.userData = @{@"isSelected":[NSNumber numberWithInt:0]}; 

今、この

-(BOOL) mapView:(GMSMapView *) mapView didTapMarker:(GMSMarker *)marker 
{ 

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 69, 60)]; 

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 69, 21)]; 

    label.text = @"Hello"; 


    label.font = [UIFont systemFontOfSize:10]; 

    label.textAlignment = NSTextAlignmentCenter; 

    label.textColor = [UIColor whiteColor]; 

    label.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:139.0/255.0 blue:58.0/255.0 alpha:1.0]; 

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, 69, 38)]; 
    NSNumber *number = [marker.userData objectForKey:@"isSelected"]; 
    if ([number integerValue] == 0) { 
     [btn setImage:[UIImage imageNamed:@"map3_"] forState:UIControlStateNormal]; 
     marker.userData = @{@"isSelected":[NSNumber numberWithInt:1]}; 
    } 
    else { 
     [btn setImage:[UIImage imageNamed:@"map2_"] forState:UIControlStateNormal]; 
     marker.userData = @{@"isSelected":[NSNumber numberWithInt:0]}; 
    } 

    [view addSubview:label]; 

    [view addSubview:btn]; 

    marker.iconView = view; 

    return YES; 
} 
関連する問題