Sidharth J Dev
Sidharth J Dev

Reputation: 987

Change the position of MyLocationButton in Google Maps in iOS

I need to change the position of MyLocationButton in Google Maps SDK that I use in my iOS Application. I tried

for view in googleMap.subviews {
            print(view.description)
            print("Hello")
        }

To get the origin.y of the Button, but that did not. Is it possible to get the coordinates?

Upvotes: 1

Views: 1065

Answers (2)

peng
peng

Reputation: 21

View myLocationButton = mapFragment.getView().findViewById(0x2);

    if (myLocationButton != null && myLocationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
        // location button is inside of RelativeLayout
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myLocationButton.getLayoutParams();

        // Align it to - parent BOTTOM|LEFT
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);

        // Update margins, set to 80dp
        final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
                getResources().getDisplayMetrics());
        params.setMargins(margin, margin, margin, margin);

        myLocationButton.setLayoutParams(params);
    }

Upvotes: 0

Nikunj Damani
Nikunj Damani

Reputation: 753

one way to do it by adding a new button over map view.

 let googleButton = UIButton(type: UIButtonType.custom) as UIButton
googleButton.setImage(UIImage(named: "RecenterButton"), for: .normal)
googleButton.frame = CGRectMake(self.view.frame.size.width-60, 100, 50, 50)
googleButton.addTarget(self, action: #selector(boundPosition), forControlEvents: .TouchUpInside)

self.view.addSubview(googleButton)
self.view.sendSubviewToBack(mapView)


func boundPosition() {
    let loc = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    let bounds = GMSCoordinateBounds(coordinate: loc, coordinate: loc)
    mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 20.0))
    mapView.animateToZoom(15)
    mapView.animateToViewingAngle(70)
}

Upvotes: 2

Related Questions