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

前端 未结 6 2113
南方客
南方客 2020-12-13 22:27

Using the Google Maps for iOS SDK, the \"My Location\" button is by default placed in the bottom right hand corner:

相关标签:
6条回答
  • 2020-12-13 22:49
    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;
                        }
                    }
                }
            };
    
        }
    }
    
    0 讨论(0)
  • 2020-12-13 22:57

    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)
    

    0 讨论(0)
  • 2020-12-13 22:57
    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;
                }
            }
    
        }
    };
    
    0 讨论(0)
  • 2020-12-13 23:01

    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 
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-13 23:02

    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];
      }
    
    0 讨论(0)
  • 2020-12-13 23:13

    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;
                }
            }
    
        }
    };
    
    0 讨论(0)
提交回复
热议问题