更改Google Maps API的"我的位置"按钮的位置

Jak*_*kob 83 java android google-maps-android-api-2 android-maps-v2

我正在使用谷歌地图Android API v2,我需要一种方法来获取"我的位置"按钮的位置.

我得到这样的"我的位置"按钮:

GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map)).getMap();

// This gets the button
map.setMyLocationEnabled(true);
Run Code Online (Sandbox Code Playgroud)

fab*_*uis 86

您可以获取"我的位置"按钮并将其移动,如:

public class MapFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mapView = super.onCreateView(inflater, container, savedInstanceState);   

    // Get the button view 
    View locationButton = ((View) mapView.findViewById(1).getParent()).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);
    rlp.setMargins(0, 0, 30, 30);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @inmyth即使我得到了相同的错误,但我使用Integer类解析了1和2.findViewById(的Integer.parseInt( "1")).如果您找到了更好的解决方案,请告诉我. (11认同)
  • 嗨@fabLouis,首先,我想感谢你.您的代码确实移动了LocationButton.我很好奇你是怎么想出那个按钮的Id?你能解释一下这个`mapView.findViewById(1).getParent()).findViewById(2);`.谢谢,SH (4认同)
  • 通过Android Studio调试器 (4认同)
  • 嗯`relativeLayout.ALIGN_PARENT_TOP`和`RelativeLayout.ALIGN_PARENT_BOTTOM`如何在右下角? (2认同)

Asc*_*ris 69

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

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

  • 这是最好的答案.findById(1)是一个糟糕的解决方案 (6认同)
  • 我更喜欢mapView.findViewWithTag("GoogleMapMyLocationButton"); 解决方案如下 (4认同)
  • 适用于将鼠标悬停按钮放在右下角,但正如你所说它会弄乱相机更新.怎么解决?截至目前,我的相机始终将屏幕底部视为中心.在计算东西时,你是否将屏幕高度的一半加到相机上? (3认同)
  • 请注意,`setPadding`还有许多其他可能不需要的副作用.最重要的是,它会改变相机目标的屏幕位置. (2认同)

Rya*_*yan 15

这可能不是最佳解决方案,但您可以将自己的按钮放在地图上并自行处理.它需要以下内容: -

1)将地图放在frameLayout中,然后在顶部添加按钮.例如

<FrameLayout
    android:id="@+id/mapFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.MapFragment"
        map:mapType="normal"
        map:uiCompass="true" />

    <ImageButton
        android:id="@+id/myMapLocationButton"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_gravity="bottom|right"
        android:background="@drawable/myMapLocationDrawable"
        android:contentDescription="My Location" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

2)编辑地图UI设置,以便在调用setMyLocationEnabled(true)时不显示该按钮.您可以通过map.getUiSettings()完成此操作.setMyLocationButtonEnabled(假);

3)处理新按钮的单击以模拟提供的按钮所执行的操作.例如,调用mMap.setMyLocationEnabled(...); 并将地图平移到当前位置.

希望有所帮助,或希望有人为您提供更简单的解决方案;-)


Sah*_*ain 14

它已经在上面解释过.只是fabLouis答案的一小部分内容.您也可以从SupportMapFragment获取地图视图.

        /**
         * Move the button
         */
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
                findFragmentById(R.id.map);
        View mapView = mapFragment.getView();
        if (mapView != null &&
                mapView.findViewById(1) != null) {
            // Get the button view
            View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(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);
        }
Run Code Online (Sandbox Code Playgroud)

  • 这一行 - ((查看)mapView.findViewById(1).getParent()).findViewById(2); 在1和2整数上给了我一个错误 - "类型ID的预期资源"然后我解决了这个问题((查看)mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer) .parseInt( "2")); (3认同)

Cor*_*ehn 12

我不想看到其他人正在使用的这些神奇的视图ID,我建议使用标签来找到MapView孩子.

这是我将" 我的位置"按钮放在" 缩放"控件上方的解决方案.

// Get map views
View location_button =_mapView.findViewWithTag("GoogleMapMyLocationButton");
View zoom_in_button = _mapView.findViewWithTag("GoogleMapZoomInButton");
View zoom_layout = (View) zoom_in_button.getParent();

// adjust location button layout params above the zoom layout
RelativeLayout.LayoutParams location_layout = (RelativeLayout.LayoutParams) location_button.getLayoutParams();
location_layout.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
location_layout.addRule(RelativeLayout.ABOVE, zoom_layout.getId());
Run Code Online (Sandbox Code Playgroud)

  • 对于想知道如何为指南针做同样事情的人来说,标签是`GoogleMapCompass`. (3认同)
  • @Randy遍历MapView(FrameLayout)的子视图并记录标签以获取它们。参见[here](https://gist.github.com/ElegyD/d1cf53be376dd29a7454167d5dbdb0bd)(写于Kotlin) (2认同)

小智 11

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

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

 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapView = mapFragment.getView();
        mapFragment.getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)

这里是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);
            }

        }
Run Code Online (Sandbox Code Playgroud)

我希望,这将解决你的问题.谢谢.


ovi*_*roa 8

请参阅以下方法.它位于一个扩展SupportMapFragment的类中.它获取按钮的容器视图,并在底部显示,水平居中.

/**
     * Move my position button at the bottom of map
     */
    private void resetMyPositionButton()
    {
        //deep paths for map controls
        ViewGroup v1 = (ViewGroup)this.getView();
        ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
        ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
        ViewGroup v4 = (ViewGroup)v3.getChildAt(1);

        //my position button
        View position =  (View)v4.getChildAt(0);

        int positionWidth = position.getLayoutParams().width;
        int positionHeight = position.getLayoutParams().height;

        //lay out position button
        RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight);
        int margin = positionWidth/5;
        positionParams.setMargins(0, 0, 0, margin);
        positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        position.setLayoutParams(positionParams);
    }
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 8

首先,获取谷歌地图视图:

 View mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getView();
Run Code Online (Sandbox Code Playgroud)

然后找到MyLocation按钮(来自Android Studio调试器的id):

 View btnMyLocation = ((View) mapView.findViewById(1).getParent()).findViewById(2);
Run Code Online (Sandbox Code Playgroud)

最后,只需为MyLocation按钮设置新的RelativeLayout参数(在这种情况下,将父级右对齐+居中对齐):

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // size of button in dp
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    params.setMargins(0, 0, 20, 0);
    btnMyLocation.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

繁荣!现在你可以随意移动它;)

  • 这一行 - ((查看)mapView.findViewById(1).getParent()).findViewById(2); 在1和2整数上给我一个错误 - "类型ID的预期资源". (3认同)
  • 试试这个 - 查看btnMyLocation =((查看)mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); (3认同)

fra*_*kee 7

如果您只想启用位置指示(蓝点)但不需要默认的"我的位置"按钮:

mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
Run Code Online (Sandbox Code Playgroud)

这样你也可以在你想要的地方画出你自己的按钮而不会有这样的奇怪的东西mapView.findViewById(1).getParent()).