我想知道是否可以将GMSMapView上的"我的位置"按钮图像更改为自定义图像。
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];
在之前的答案中(使用从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)
}