如何在安卓系统中移动谷歌地图缩放控件的位置?

8
我设法使谷歌地图正常运行。问题是我正在开发的应用程序当前的设计与谷歌地图中的缩放控件重叠。我该如何将此缩放控件移动到另一个位置?

enter image description here


如果您想将现有的默认缩放控制器移动到其他位置,我可以帮助您。 - Zumry Mohamed
2个回答

9
我找到了两种移动Google地图缩放控件的方法。第一种方法是在@Numan1617的回答中提到的,使用setPadding()函数:
googleMap.setPadding(left, top, right, bottom);

当您在屏幕底部放置Google AdView横幅广告时,使用此方法非常有用,可以确保缩放控件可见。就像这样:

googleMap.setPadding(0,0,0,adViewLayout.getHeight());

这将移动左下角的Google名称、缩放控件和工具栏(当您触摸地图上的标记时,它会从右到左滑入),并将它们放置在包含AdView的布局正上方。否则,这些UI控件可能完全或部分位于 AdView 后面。

但是,如果您想要比那更精细的控制呢?比如将缩放控件移动到角落里?因此,我深入研究并找到了一种方法:获取包含缩放控件的视图对象,并为其设置新的LayoutParams。

我还想移动指南针和工具栏,因此在此处包括了这些代码。

注意:GoogleMap UI控件具有设置文本标签的功能,使其便于访问。我包括了我能找到的所有UI控件文本标签。

缩放控件封装布局上似乎没有方便的标签,因此我必须获取其中一个缩小/放大控件,然后获取其父视图(实际上是ViewGroup)。我发现它包含RelativeLayout.LayoutParams,这意味着它的封装布局是RelativeLayout。

这是在DisplayMapFragment.java中:

package <blah>;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

public class DisplayMapFragment implements OnMapReadyCallback {
    private static final String TAG = DisplayMapFragment.class.getSimpleName();

    private static final String GOOGLEMAP_COMPASS = "GoogleMapCompass";                   // [4]
    private static final String GOOGLEMAP_TOOLBAR = "GoogleMapToolbar";                   // [3]
    private static final String GOOGLEMAP_ZOOMIN_BUTTON = "GoogleMapZoomInButton";        // [2]child[0]
    private static final String GOOGLEMAP_ZOOMOUT_BUTTON = "GoogleMapZoomOutButton";      // [2]child[1]
    private static final String GOOGLEMAP_MYLOCATION_BUTTON = "GoogleMapMyLocationButton";// [0]

    private View mapView = null;

    public DisplayMapFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        Log.d(TAG, "onCreateView()");

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

        mapFragment.setRetainInstance(true);

        mapFragment.getMapAsync(this);  // map calls my onMapReady()

        mapView = mapFragment.getView();

        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "onMapReady()");

        // move the compass to top center
        moveCompass(mapView,-1,4,-1,-1,true,false);

        // move toolbar to right center
        moveToolbar(mapView,-1,-1,4,-1,false,true);

        // move zoom controls to bottom right
        moveZoomControls(mapView, -1,-1,4,4,false,false);
    }

    /**
     * Move the View according to the passed params.  A -1 means to skip that one.
     *
     * NOTE:  this expects the view to be inside a RelativeLayout.
     *
     * @param view - a valid view
     * @param left - the distance from the left side
     * @param top - the distance from the top
     * @param right - the distance from the right side
     * @param bottom - the distance from the bottom
     * @param horizontal - boolean, center horizontally if true
     * @param vertical - boolean, center vertically if true
     */
    private void moveView(View view, int left, int top, int right, int bottom, boolean horizontal, boolean vertical) {
        try {
            assert view != null;

            // replace existing layout params
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            if (left >= 0) {
                rlp.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            }

            if (top >= 0) {
                rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            }

            if (right >= 0) {
                rlp.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
                rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            }

            if (bottom >= 0) {
                rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            }

            if (horizontal) {
                rlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            }

            if (vertical) {
                rlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            }

            rlp.setMargins(left, top, right, bottom);

            view.setLayoutParams(rlp);
        } catch (Exception ex) {
            Log.e(TAG, "moveView() - failed: " + ex.getLocalizedMessage());
            ex.printStackTrace();
        }
    }

    private void moveCompass(View mapView, int left, int top, int right, int bottom, boolean horizontal, boolean vertical) {

        assert mapView != null;

        View compass = mapView.findViewWithTag(GOOGLEMAP_COMPASS);

        if (compass != null) {
            moveView(compass,left,top,right,bottom,horizontal,vertical);
        }
    }

    private void moveToolbar(View mapView, int left, int top, int right, int bottom, boolean horizontal, boolean vertical) {

        assert mapView != null;

        View toolbar = mapView.findViewWithTag(GOOGLEMAP_TOOLBAR);

        if (toolbar != null) {
            moveView(toolbar,left,top,right,bottom,horizontal,vertical);
        }
    }

    private void moveZoomControls(View mapView, int left, int top, int right, int bottom, boolean horizontal, boolean vertical) {

        assert mapView != null;

        View zoomIn = mapView.findViewWithTag(GOOGLEMAP_ZOOMIN_BUTTON);

        // we need the parent view of the zoomin/zoomout buttons - it didn't have a tag
        // so we must get the parent reference of one of the zoom buttons
        View zoomInOut = (View) zoomIn.getParent();

        if (zoomInOut != null) {
            moveView(zoomInOut,left,top,right,bottom,horizontal,vertical);
        }
    }
}

3
您可以使用GoogleMap.setPadding()为地图上显示的控件添加填充。
更多文档请参阅此处

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接