How to add a button on the Google maps in iOS?

后端 未结 5 2055
北海茫月
北海茫月 2021-02-05 16:44

I am new to iOS Programming and I have downloaded the google maps sdk for iOS and followed the instruction on their website ( as shown in this link https://developers.google.com

相关标签:
5条回答
  • 2021-02-05 17:10

    I faced the same issue and the fix is very easy, just override this UIViewController method:
    -(void) viewWillAppear:(BOOL)animated and put your UIButton creation logic inside that method.

    Example:

    -(void) viewWillAppear:(BOOL)animated
    {
     locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
     locationButton.frame = CGRectMake(0, 30, self.view.frame.size.width/6, self.view.frame.size.height/6);
     [locationButton setImage:[UIImage imageNamed:@"location_enabled.png"] forState:UIControlStateNormal];
     [self.view addSubview:locationButton];
    }
    
    0 讨论(0)
  • 2021-02-05 17:15

    Make your view normally with InterfaceBuilder and to put mapView at 0 index subview :

    mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
    mapView_.myLocationEnabled = YES;
    [self.view insertSubview:mapView_ atIndex:0];
    

    Thanks to Raul Clauss from this thread

    And you can custom the size of mapView, change :

    mapView_ = [GMSMapView mapWithFrame:_mapHolder.bounds camera:camera];
    mapView_.myLocationEnabled = YES;
    [_mapHolder insertSubview:mapView_ atIndex:0];
    

    Where mapHolder, is a UIView made inside the IB.

    0 讨论(0)
  • 2021-02-05 17:26

    This problem occur because you try to add the map view with the code, earlier you add the button with the help of interface builder. When you add the mapView it would add at the top of the view that's why your button is not shown.

    You have two options for doing this.

    First approach

    • Drag and drop the UIButton from the interface builder and set the constraints accordingly

    • Then create the outlet of that button in the corresponding viewController

      @IBOutlet weak var bottonToAdd: UIButton!

    • After adding the map view to the view paste the blow code to bring the button at the top mapViewForScreen.addSubview(self.bottonToAdd) mapViewForScreen.bringSubviewToFront(self.bottonToAdd)

    After the above three step you can find out that you hav the button over mapview

    Second approach

    If you don't want to use the interface builder you can use the below code for the same.

        var bottonToAdd:UIButton = UIButton()
        bottonToAdd.setTitle("Button To Add", for: .normal)
        bottonToAdd.sizeToFit()
        bottonToAdd.center = mapViewForScreen.center
        bottonToAdd.tintColor = UIColor.blue
        mapViewForScreen.addSubview(bottonToAdd)
        mapViewForScreen.bringSubviewToFront(bottonToAdd)
    
    0 讨论(0)
  • 2021-02-05 17:27
     let mapButton:UIButton = UIButton(frame: CGRect(x:self.view.bounds.width-50, y: 20, width: 40, height: 40)) //Swfit 5
        mapButton.backgroundColor = UIColor.clear
        mapButton.setImage(UIImage(named: "list"), for: UIControl.State.normal)
        mapButton.setImage(UIImage(named: "map"), for: UIControl.State.selected)
        mapButton.addTarget(self, action:#selector(self.mapListClicked), for: .touchUpInside)
        self.view.addSubview(mapButton)
    
    0 讨论(0)
  • 2021-02-05 17:29

    GMSMapView is subclass of UIView so you can add subviews as to any other view

    Try this code

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(mapView_.bounds.size.width - 110, mapView_.bounds.size.height - 30, 100, 20);
    button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
    [button setTitle:@"Button" forState:UIControlStateNormal];
    [mapView_ addSubview:button];
    

    It adds 100x20 button as the subview of the GMSMapView, positioned to the bottom right corner. I have tested it and the button can be touched as in any other view

    Edit: Also move all your code from -loadView to -viewDidLoad. -loadView method is never called when using IB to create UI. Docs to -loadView says:

    If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

    Edit 2: I believe when you create view hierarchy using Interface Builder, you CAN NOT reset self.view property like you are doing.

    Do this in your -viewDidLoad

    [self.view addSubview: mapView_];
    

    instead of

    self.view = mapView_;
    

    if you are passing GMSMapView to the self.view property, the map is only view which is in the controller from this point. Thats, I believe, the reason why u can't see your IB-created button.

    0 讨论(0)
提交回复
热议问题