Google Maps iOS SDK move “My Location” button to bottom left hand corner

倖福魔咒の 提交于 2019-11-28 18:58:59

The easiest way to solve this is to change the frame and autoresizing mask of the button right after the map view is created.

UIButton* myLocationButton = (UIButton*)[[mapView_ subviews] lastObject];
myLocationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
CGRect frame = myLocationButton.frame;
frame.origin.x = 5;
myLocationButton.frame = frame;

EDIT: Sorry, I placed the button in the top right corner. Updated my code to place it in the bottom left hand corner.

Note: there is currently no API from Google to get the location button. The first line may not work in future releases of the SDK.

You also should create a feature request here.

Another option is to create the button yourself and add it as a subview to the map. The button handler code is fairly easy:

  CLLocation *location = mapView_.myLocation;
  if (location) {
    [mapView_ animateToLocation:location.coordinate];
  }

You can use the padding of the GMSMapView.

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969
                                                        longitude:144.966085
                                                             zoom:4];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

_mapView.settings.myLocationButton = YES;
_mapView.myLocationEnabled = YES;
_mapView.padding = UIEdgeInsetsMake(0, 0, kOverlayHeight, 0);
self.view = _mapView;

SWIFT 3

self._mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)

in swift 3 xcode 8

func moveLocationButton() -> Void{
     for object in mapView.subviews{
        for obj in object.subviews{
           if let button = obj as? UIButton{
             let name = button.accessibilityIdentifier
                if(name == "my_location"){
                        //config a position
                        button.center = self.view.center 
                    }
                }
            }
        }
    }
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 -= 60;
                view.frame = frame;
            }
        }

    }
};

As of July 2015, this is a way to do it:

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

    }
};
for (UIView *object in _mapView.subviews) {
    if([[[object class] description] isEqualToString:@"GMSUISettingsPaddingView"] )
    {
        for (UIView *settingView in object.subviews) {
            if([[[settingView class] description] isEqualToString:@"GMSUISettingsView"] )
            {
                for(UIView *view in settingView.subviews) {
                    if([[[view class] description] isEqualToString:@"GMSx_QTMButton"] ) {
                        CGRect frame = view.frame;
                        frame.origin.y -= 60;
                        view.frame = frame;
                    }
                }
            }
        };

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!