【问题标题】:Left align My Location button on Google Map Android左对齐 Google Map Android 上的“我的位置”按钮
【发布时间】:2019-02-08 13:58:44
【问题描述】:

我一直在尝试将谷歌地图上的我的位置按钮左对齐,因为它默认右对齐。

我已经看到解决方案代码here,但没有多大帮助。

并尝试了以下代码:

SupportMapFragment mapFragment = ((SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.fragmentMap));
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();
    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 10dp
    final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    params.setMargins(margin, margin, margin, margin);
    myLocationButton.setLayoutParams(params);
}

【问题讨论】:

    标签: java android google-maps location-button


    【解决方案1】:

    您应该在parent.post() 方法中执行此操作。试试这个代码:

    void myLocationBottomLeftAlign() {
        try {
            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
            mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
    
            final ViewGroup parent = (ViewGroup) mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
            parent.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        Resources r = getResources();
                        int marginPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
    
                        View mapLocation = mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton");
    
                        // create layoutParams, giving it our wanted width and height(important, by default the width is "match parent")
                        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(mapLocation.getHeight(),
                                mapLocation.getHeight());
                        // position on top right
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                        //give compass margin
                        rlp.setMargins(marginPixels, marginPixels, marginPixels, marginPixels);
                        mapLocation.setLayoutParams(rlp);
    
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    

    onMapReady() 中调用它,不要忘记位置权限:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
            if (locationPermission == PackageManager.PERMISSION_GRANTED) {
                myLocationBottomLeftAlign();
            } else {
                // request permission and than call myLocationBottomLeftAlign();
    
            }
        } else {
            myLocationBottomLeftAlign();
        }
    }
    

    【讨论】:

    • 它工作了,但做了一个小的改动。在if (locationPermission != PackageManager.PERMISSION_GRANTED) 中将!= 替换为==
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2012-07-05
    • 2015-07-04
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    相关资源
    最近更新 更多