【问题标题】:How to re-position my location button in google mapv2 in android?如何在android的谷歌地图v2中重新定位我的位置按钮?
【发布时间】:2013-07-26 14:44:34
【问题描述】:

我正在使用 Google map-V2 API 在 android 中编写应用程序。我想像在谷歌地图应用程序中一样覆盖操作栏。我启用了“我的位置”按钮。现在的问题是我的位置按钮位于操作栏下方。有没有办法重新定位这个按钮。我想制作一个类似于地图的应用程序。我是android新手,所以请帮忙。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您可以将padding 设置为地图。

    这解决了问题 - 覆盖在顶部的地图上(我使用了 48 个下降,但您可以覆盖操作栏,然后获取它的实际高度 (?android:attr/actionBarSize))

    mapFragment.getMap().setPadding(0, dpToPx(48), 0, 0);
    

    (DP转PX功能来自this SO answer)

    【讨论】:

    • 以编程方式设置填充对我有用,而在布局 xml 中设置却没有
    【解决方案2】:

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

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

    【讨论】:

    • 底部如何设置 |离开了吗?
    • 你必须使用aMap.setPadding。该方法负责设置Location image ICON的位置。
    【解决方案3】:

    您不能以任何方式更改 MyLocationButton,但可以启用和禁用它。已经有一个request 用于此。

    请随时disable 按钮并实现您自己的按钮。

    你会有这样的东西:

    mMap.getUiSettings().setMyLocationButtonEnabled(false);
    

    【讨论】:

    • 请帮忙,我怎样才能在地图上添加一个按钮。以及如何从新添加的按钮中获取我的位置。
    • 最好就此提出一个新问题。
    • 如果我禁用 MyLocationButton 如何以类似的方式显示位置?
    • 一定要调用mMap.setMyLocationEnabled(true);
    • 对不起...这对我有什么帮助。这不会启用按钮吗?
    【解决方案4】:

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

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

       SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapView = mapFragment.getView();
            mapFragment.getMapAsync(this);
    

    这里是 onMapReady() 代码:-

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

    希望这能解决您的问题。谢谢。

    【讨论】:

      【解决方案5】:

      我没有使用 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);
      

      【讨论】:

        【解决方案6】:

        只需使用 GoogleMap.setPadding(left, top, right, bottom),它允许您指示可能被其他视图遮挡的地图部分。设置填充会重新定位标准地图控件,并且相机更新将使用填充区域。

        https://developers.google.com/maps/documentation/android/map#map_padding

        【讨论】:

        • 不错的把戏。任何方式如何获得当前填充? GoogleMap.getPadding method() 不存在...
        猜你喜欢
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 2013-08-09
        • 2016-04-22
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        相关资源
        最近更新 更多