【问题标题】:Make Google Maker like a Toggle Button让 Google Maker 像一个切换按钮
【发布时间】:2016-12-22 14:13:43
【问题描述】:

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

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

【问题讨论】:

  • 只需在方法 didTapMarker 中唯一标识您的标记,然后相应地执行您的自定义操作..

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


【解决方案1】:

您可以为此使用GMSMarkeruserData 属性,首先使用所有标记设置userData,因此将此行添加到viewDidLoad 的for 循环中,您将在Map 上添加标记。

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

【讨论】:

    猜你喜欢
    • 2020-08-24
    • 2021-09-13
    • 2011-01-22
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2014-11-30
    相关资源
    最近更新 更多