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

4
我想知道是否可以将GMSMapView上的"我的位置"按钮图像更改为自定义图像。
3个回答

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

在之前的答案中(使用从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)
}

0
为了实现这一点,您可以从GMSMapView中检索到该按钮的UIButton引用。之后,您只需使用标准的[UIButton setImage:]来设置所有不同状态下的图像。我创建了一个类别,用于从GMSMapView控件获取此UIButton:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接