GMSMapView自定义我的位置按钮图像

时间:2014-11-05 12:42:46

标签: ios gmsmapview

我想知道是否可以将我的位置按钮图像从GMSMapView更改为自定义图像。

3 个答案:

答案 0 :(得分:4)

1)了解帧位置按钮:

   for (UIView *object in self.mapView.subviews) {
    if([[[object class] description] isEqualToString:@"GMSUISettingsView"] )
    {
        for(UIView *view in object.subviews) {
            if([view.accessibilityIdentifier isEqual:@"my_location"] ) {

                frame = view.frame;

            }
        }

    }
   }

使用debug查看正确的帧!!!

2)不要使用self.mapView.settings.myLocationButton = YES;

3)为此框架创建自己的按钮:

    UIButton *buttonLocation = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonLocation.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 66, [UIScreen mainScreen].bounds.size.height - frame.size.height, frame.size.width, frame.size.height);
[buttonLocation addTarget:self
           action:@selector(bLocations:)
 forControlEvents:UIControlEventTouchUpInside];
[buttonLocation setImage:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

[self.mapView addSubview:buttonLocation];

答案 1 :(得分:0)

为此,您可以从GMSMapView检索对该按钮的UIButton引用。之后,您只需对所有不同的状态使用标准[UIButton setImage:]。我创建了一个从GMSMapView控件获取此UIButton的类别:

https://github.com/trant/GMSMapView-Additions

答案 2 :(得分:0)

根据先前的答案(使用从GMSMapView获取UIButton引用的方法),这是Swift中的更新答案。它将默认的“我的位置”图标替换为您自己的自定义图片。

private func setupMyLocationButton() {
  let locationButton = mapView.subviews
    .filter { $0.description.contains("GMSUISettingsPaddingView") }
    .flatMap { $0.subviews }
    .flatMap { $0.subviews }
    .filter { $0.description.contains("GMSx_QTMButton") }
    .first
  let customImage = UIImage(imageLiteralResourceName: "yourCustomIconFilename")
  let myLocationButton = locationButton as? UIButton
  myLocationButton?.setImage(customImage, for: .normal)
}