代码之家  ›  专栏  ›  技术社区  ›  Lovepreet Singh

谷歌地图Android上的“左对齐我的位置”按钮

  •  0
  • Lovepreet Singh  · 技术社区  · 6 年前

    我一直在尝试左对齐 我的位置 默认情况下,谷歌地图上的右对齐按钮。

    我看过解决方案代码 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);
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Andrii Omelchenko    6 年前

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