4

I would like to know if it is possible to change the my location button image from GMSMapView to a custom image.

apinho
  • 2,235
  • 3
  • 25
  • 39

3 Answers3

4

1) Know frame location button:

   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;

            }
        }

    }
   }

Use debug to see correct frame!!!

2) Don't use self.mapView.settings.myLocationButton = YES;

3) Create own button to this frame:

    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];
AndrewSas
  • 683
  • 5
  • 10
1

Following the previous answers (using the method of getting the UIButton reference from the GMSMapView), here is an updated answer in Swift. It replaces the default My Location icon with your own custom image.

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)
}
angtlin
  • 192
  • 1
  • 9
0

In order to do that, you can retrieve the UIButton reference to that button from the GMSMapView. After that you just use the standard [UIButton setImage:] for all the different states. I created a category for getting this UIButton from the GMSMapView control:

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

apinho
  • 2,235
  • 3
  • 25
  • 39