How to change location of my location button in google maps using swift

20,026

Solution 1

You can change the position of the MapControls by set the padding for your map view .

 let mapView = GMSMapView()

 mapView.isMyLocationEnabled = true
 mapView.settings.myLocationButton = true

 mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 50)

It will change the padding of the my location control. From bottom 50px and from right 50px

Refer the below screenshot which added the padding of 50px from bottom and right.

enter image description here

Solution 2

Like Subramanian, this works very well :

mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)

This is perfect if you have a navigation bar in your app.

Solution 3

Update for swift 4 : just change the position of my location button only

for object in self.mapView.subviews{
            if(object.theClassName == "GMSUISettingsPaddingView"){
                for view in object.subviews{
                    if(view.theClassName == "GMSUISettingsView"){
                        for btn in view.subviews{
                            if(btn.theClassName == "GMSx_QTMButton"){
                                var frame = btn.frame
                                frame.origin.y = frame.origin.y - 100
                                btn.frame = frame

                            }
                        }
                    }
                }
            }
        }

    public extension NSObject {
    public var theClassName: String {
        return NSStringFromClass(type(of: self))
    }
}

Solution 4

Here's my function for Google Maps SDK 5.1.0, since the button's class was renamed to 'GMSx_MDCFloatingButton' in case someone needs an updated approach.

Also depending on your implementation you might want to call this on viewDidAppear().

func moveLocationButton(){
        for object in self.googleMapsView!.subviews{
            if(object.thisClassName == "GMSUISettingsPaddingView"){
                for view in object.subviews{
                    if(view.thisClassName == "GMSUISettingsView"){
                        for btn in view.subviews{
                            if(btn.theClassName == "GMSx_MDCFloatingButton"){
                                var frame = btn.frame
                                frame.origin.x = frame.origin.x - 50
                                frame.origin.y = frame.origin.y - 150
                                btn.frame = frame
                                
                            }
                        }
                    }
                }
            }
        }
    }

    public extension NSObject {
    var thisClassName: String {
        return NSStringFromClass(type(of: self))
       }
    }
Share:
20,026

Related videos on Youtube

Bhanuteja
Author by

Bhanuteja

Updated on October 25, 2021

Comments

  • Bhanuteja
    Bhanuteja over 2 years

    my location button

    I am using google maps in my project i want to show my location button a bit up how should i do that using swift

    • Developer
      Developer almost 7 years
      @ Amma teja :- I don't think you can change the position of my location button, but what you can do is, create your own button and give it whatever frame you want.
  • Arafin Russell
    Arafin Russell over 6 years
    but as you can see the position of current location also shift, but I want to keep the current location centre while moving the myLocationButton upward. How can I do that please ?
  • Bhanuteja
    Bhanuteja over 6 years
    @subramanian: have small doubt here, i want to change the position of my location button only. not "Google" text also
  • Hardik1344
    Hardik1344 about 5 years
    For this once you change the location button and press the button ,current location will not come on centre.
  • Jose Tovar
    Jose Tovar about 5 years
    How to enable map toolbar... ?
  • Taimur Ajmal
    Taimur Ajmal over 4 years
    can we hide this button ?
  • Starwave
    Starwave over 2 years
    Calling it on "viewDidAppear" will cause the button to jump. So I called this in "viewDidLoad" with a slight delay: "DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)"