JP Illanes
JP Illanes

Reputation: 3675

How to change myLocationButton position from a GMSMapView?

Anybody know how to get the instance of the myLocationButton from a GMSMapView instance? Or a way to change the default position? I just need to move it some pixels up.

Upvotes: 5

Views: 5167

Answers (4)

Henrique Carvalho
Henrique Carvalho

Reputation: 113

Swift 2.3: Solution to move only the Location Button. I am using pod 'GoogleMaps', '~> 2.1'.

for object in mapView.subviews {
    if object.theClassName == "GMSUISettingsView" {
        for view in object.subviews {
            if view.theClassName == "GMSx_QTMButton" {
              var frame = view.frame
              frame.origin.y = frame.origin.y - 110px // Move the button 110 up
              view.frame = frame
            }
        }
    }
}

Extension to get the Class Name.

extension NSObject {
  var theClassName: String {
    return NSStringFromClass(self.dynamicType)
  }
}

I will update this code to Swift 3.0 as soon as possible. :)

Upvotes: 3

Alexander Scholz
Alexander Scholz

Reputation: 2100

Raspu's solution moves the whole GMSUISettingsView including the compass.

I found a solution to move only the Location Button:

for (UIView *object in mapView_.subviews) {
    if([[[object class] description] isEqualToString:@"GMSUISettingsView"] )
    {
        for(UIView *view in object.subviews) {
            if([[[view class] description] isEqualToString:@"UIButton"] ) {
                CGRect frame = view.frame;
                frame.origin.y -= 75;
                view.frame = frame;
            }
        }
        /*        
        CGRect frame = object.frame;
        frame.origin.y += 75;
        object.frame = frame;
        */
    }
};

If you uncomment the three lines you move only the compass (for some reason I'm not able to move the GMSCompassButton View).

Upvotes: 4

user1312972
user1312972

Reputation: 166

According the the issue tracker for this Issue 5864: Bug: GMSMapView Padding appears not to work with AutoLayout there is an easier workaround:

- (void)viewDidAppear:(BOOL)animated {
  // This padding will be observed by the mapView
  _mapView.padding = UIEdgeInsetsMake(64, 0, 64, 0);
}

Upvotes: 15

JP Illanes
JP Illanes

Reputation: 3675

Right now the only way I have found is this way:

//The method 'each' is part of Objective Sugar 
[googleMapView.subviews each:^(UIView *object) {
    if([[[object class] description] isEqualToString:@"GMSUISettingsView"] )
    {
        CGPoint center = object.center;
        center.y -= 40; //Let's move it 40px up
        object.center = center;
    }
}];

It's working fine, but an official way would be better.

This works for 1.4.0 Version. For previous versions, change @"GMSUISettingsView" for @"UIButton".

Upvotes: 0

Related Questions