代码之家  ›  专栏  ›  技术社区  ›  Chirag

如何更改位置或删除谷歌地图的默认GPS图标?

  •  0
  • Chirag  · 技术社区  · 7 年前

    我是谷歌地图及其API的新手。在这里,我加载了谷歌地图,并试图更改附加图像中GPS图标的位置,但它没有从其位置移动。

    我已使用加载地图 SupportMapFragment 并已使用 getMapAsync 在中获取贴图对象的方法 onMapReady() 方法我知道如果我删除getMapAsync,该图标将被删除,但如果没有它,我就无法获得map的对象。这就是我面临的问题。

    是否需要移动或移除它的位置。 我们将不胜感激。非常感谢。

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  1
  •   Kapil Parmar    7 年前

    删除 Location Button 在onMapReady中添加

    map = googleMap;
            map.getUiSettings().setMyLocationButtonEnabled(false);
    
        2
  •  1
  •   Naveen Kumar M    7 年前

    您可以通过从片段中获取当前位置按钮的视图来更改此设置

    //Fragment code to change the position of the current location button
    private SupportMapFragment mapFragment;
     mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                    .findFragmentById(R.id.map);
    
     @Override
        public void onMapReady(GoogleMap googleMap) {
            try {
                map = googleMap;
    
                if (mapFragment != null &&
                        mapFragment.getView().findViewById(Integer.parseInt("1")) != null) {
                    // Get the button view
                    View locationButton = ((View) mapFragment.getView().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_BOTTOM, RelativeLayout.TRUE);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START, 0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
                    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, 0);
                    int marginBottom = mNavigationListFragment.llTypeContainer.getHeight() + 5;
                    int marginRight = (int)convertFromDpToPixel(context, 10);
                    layoutParams.setMargins(0, 0, marginRight, marginBottom);
                }
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
    public static final float convertFromDpToPixel(Context context, float dp) {
            float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
            return ht_px;
        }
    

    要隐藏该按钮,请使用以下代码:

    @Override
        public void onMapReady(GoogleMap googleMap) {
            map = googleMap;
            map.getUiSettings().setMyLocationButtonEnabled(false);
        }