使Google Maker像切换按钮一样

时间:2016-08-16 09:13:32

标签: ios objective-c google-maps google-maps-markers

实际上我正在将iconview添加到标记,并在点击该标记时更改为另一个iconview。这可以通过在didTapMarker委托方法中添加iconview轻松完成。但是当一个人选择另一个Marker时如何更改为默认视图。就像Toggle Button

一样
- (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;            
    }
}

并在didTapMarker委托方法

-(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;
}

1 个答案:

答案 0 :(得分:1)

您可userData GMSMarker属性,首先使用您的所有标记设置userData,因此请在viewDidLoad的for循环中添加此行,以便在Map添加标记{1}}。

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

现在didTapMarker点击这样

-(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;
}
相关问题