如何在android的google mapv2中重新定位我的位置按钮?

如何在android的google mapv2中重新定位我的位置按钮?,android,google-maps,Android,Google Maps,我正在用android编写一个使用GoogleMap-v2API的应用程序。我想像在谷歌地图应用程序中一样覆盖操作栏。我启用了“我的位置”按钮。现在的问题是我的位置按钮在操作栏下面。有没有办法重新定位这个按钮。我想做一个类似地图的应用程序。我是android新手,请帮助。您不能以任何方式更改MyLocationButton,只能启用和禁用它。已经有了一个解决方案 随意点击按钮,只需实现您自己的一个 你会有这样的想法: mMap.getUiSettings().setMyLocationButto

我正在用android编写一个使用GoogleMap-v2API的应用程序。我想像在谷歌地图应用程序中一样覆盖操作栏。我启用了“我的位置”按钮。现在的问题是我的位置按钮在操作栏下面。有没有办法重新定位这个按钮。我想做一个类似地图的应用程序。我是android新手,请帮助。

您不能以任何方式更改MyLocationButton,只能启用和禁用它。已经有了一个解决方案

随意点击按钮,只需实现您自己的一个

你会有这样的想法:

mMap.getUiSettings().setMyLocationButtonEnabled(false);
你可以设置为地图

这就解决了问题——覆盖了顶部的地图(我使用了48个凹陷,但是你可以覆盖动作栏,然后得到它的实际高度(?android:attr/actionBarSize))


(DP to PX函数是from)

只需使用GoogleMap.setPadding(左、上、右、下),它允许您指示地图中可能被其他视图遮挡的部分。设置填充将重新定位标准地图控件,相机更新将使用填充区域


您可以轻松地重新定位位置按钮

View plusMinusButton = suppormanagerObj.getView().findViewById(1);
View locationButton = suppormanagerObj.getView().findViewById(2);

// and next place it, for exemple, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams)     locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
也可以在设置“位置”按钮的对齐方式后设置填充

mMap.setPadding(0, 0, 30, 105); 

我在地图片段中解决了这个问题,使用下面的代码将我的位置按钮重新定位到视图的右下角, 以下是my Maps Activity.java:-

在onCreate()方法中添加这行代码

下面是onMapReady()代码:-


我希望这能解决你的问题。谢谢。

我没有使用findViewById解决方案,而是使用findViewWithTag获取按钮的引用。对我来说,使用字符串似乎更具可读性和可靠性

    View myLocationButton = mMap.findViewWithTag("GoogleMapMyLocationButton");
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) myLocationButton.getLayoutParams();
    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

请帮助,我如何在地图顶部添加按钮。以及如何从新添加的按钮获取我的位置。最好打开一个新问题。如果禁用MyLocationButton,如何以类似方式显示位置?请务必调用mMap.setMyLocationEnabled(true);对不起……这对我有什么帮助。这不是可以启动按钮吗?好把戏。有没有办法获得当前的填充?GoogleMap.getPadding方法()不存在…通过编程设置填充对我来说很有效,而在布局xml中设置填充不需要在左下角设置?您必须使用aMap.setPadding。此方法负责设置位置图像图标的位置。
   SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapView = mapFragment.getView();
        mapFragment.getMapAsync(this);
  @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.setMyLocationEnabled(true);

            // Add a marker in Sydney and move the camera
            LatLng sydney = new LatLng(-34, 151);
            mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

            if (mapView != null &&
                    mapView.findViewById(Integer.parseInt("1")) != null) {
                // Get the button view
                View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
                // and next place it, on bottom right (as Google Maps app)
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                        locationButton.getLayoutParams();
                // position on right bottom
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
                layoutParams.setMargins(0, 0, 30, 30);
            }

        }
    View myLocationButton = mMap.findViewWithTag("GoogleMapMyLocationButton");
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) myLocationButton.getLayoutParams();
    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);