Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 659
Bhanuteja Avatar asked Jun 27 '17 10:06

Bhanuteja


4 Answers

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

like image 77
Subramanian P Avatar answered Nov 09 '22 15:11

Subramanian P


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))
    }
}
like image 7
Trọng Nghĩa Ngô Avatar answered Nov 09 '22 16:11

Trọng Nghĩa Ngô


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.

like image 6
XenoX Avatar answered Nov 09 '22 15:11

XenoX


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))
       }
    }
like image 3
ikanimai Avatar answered Nov 09 '22 17:11

ikanimai