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
-
1@ 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. – Developer Jun 27 '17 at 10:13
6 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.

- 4,365
- 2
- 21
- 25
-
2but 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 ? – Arafin Russell Oct 29 '17 at 20:20
-
2@subramanian: have small doubt here, i want to change the position of my location button only. not "Google" text also – Bhanuteja Jan 22 '18 at 10:29
-
For this once you change the location button and press the button ,current location will not come on centre. – Hardik1344 May 11 '19 at 06:05
-
-
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))
}
}

- 71
- 1
- 1
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.

- 409
- 6
- 14
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))
}
}

- 47
- 1
- 8
-
1Calling 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)" – Starwave Feb 15 '22 at 10:15
I would recommend to change button position by correcting constraint instead of changing frame.origin
. I got it worked by changing frame.origin
placing it into viewDidAppear
only. Neither viewWillApper
nor viewWillLayoutSubviews
were correct methods to call from. However I managed it work perfectly calling constraint changing from viewDidLoad
. And I used snapkit, but it is not a big deal to get it done natively
private func changeCurrentLocationButtonPosition(of mapView: GMSMapView) {
for object in mapView.subviews where object.theClassName == "GMSUISettingsPaddingView" {
for view in object.subviews where view.theClassName == "GMSUISettingsView" {
for btn in view.subviews where btn.theClassName == "GMSx_MDCFloatingButton" {
btn.snp.makeConstraints({ maker in
maker.left.equalToSuperview().offset(15)
})
btn.snp.makeConstraints({ maker in
maker.bottom.equalToSuperview().offset(-30)
})
return
}
}
}
}

- 2,565
- 1
- 11
- 21
Use this function and call this at viewDidLayoutSubviews()
func changeLocationBtnFrame(){
self.mapView.settings.myLocationButton = true
for obj in self.mapView.subviews{
if String(describing: obj.classForCoder) == "GMSUISettingsPaddingView"{
for viw in obj.subviews{
if String(describing: viw.classForCoder) == "GMSUISettingsView"{
for btn in viw.subviews{
if String(describing: btn.classForCoder) == "GMSx_MDCFloatingButton"{
var frame = btn.frame
btn.frame = CGRect(x: 316, y: 0, width: 56, height: 56)
//USE you want frame this line
break
}
}
break
}
}
break
}
}
}

- 1
- 2