在iOS中的Google地图中更改MyLocationButton的位置

时间:2017-02-01 11:42:03

标签: ios swift google-maps-sdk-ios

我需要更改我在iOS应用中使用的Google Maps SDK中MyLocationButton的位置。我试过了

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

获取Button的origin.y,但事实并非如此。是否可以获得坐标?

2 个答案:

答案 0 :(得分:2)

通过在地图视图上添加新按钮来实现此目的。

 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)
}

答案 1 :(得分:0)

查看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);
    }