Android: Google 地图 API - 更改地图工具栏的位置
Android: Google Maps API - Change position of maps toolbar
我正在为 Android 使用 Google 地图 API。当我点击一个 Marker
时,以下代码会启用右下角的地图工具栏
googleMap.getUiSettings().setMapToolbarEnabled(true);
但是,我希望地图工具栏显示在地图的右上角,而不是默认的右下角位置。
我该怎么做?
想发表评论,但名气不高。所以:
据我所知,暂时没有办法定位。它只会出现在默认位置。
正如 Nathan 所说,如果可能,我应该添加一些替代方案。所以,有一个解决方法:您可以禁用地图的工具栏。并且可以在布局中的任何位置添加 appcompact 的工具栏。
参考:https://developer.android.com/reference/android/support/v7/widget/Toolbar.html
这是我之前遇到的关于如何将工具栏移动到页面底部的答案。希望对你有帮助。
//get reference to my location icon
View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("2"));
// and next place it, for example, 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);
编辑:
根据(@Andro 的)建议,这是更改地图工具栏位置的代码:
//获取对我的位置图标的引用
View toolbar = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("4"));
// and next place it, for example, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.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);
2021 年更新
您应该能够通过设置 googleMap 对象上的填充来移动地图 UI 控件(罗盘、我的位置、缩放等)。这是我发现的唯一提供的内部方式。这是来自文档的信息:
Padding can be helpful when designing UIs that overlap some portion
of the map. For example, in the below image, the map is padded along
the top and right edges. Visible map controls and legal text will be
displayed along the edges of the padded region
https://developers.google.com/maps/documentation/android-sdk/map#map_padding
我正在为 Android 使用 Google 地图 API。当我点击一个 Marker
时,以下代码会启用右下角的地图工具栏googleMap.getUiSettings().setMapToolbarEnabled(true);
但是,我希望地图工具栏显示在地图的右上角,而不是默认的右下角位置。
我该怎么做?
想发表评论,但名气不高。所以:
据我所知,暂时没有办法定位。它只会出现在默认位置。
正如 Nathan 所说,如果可能,我应该添加一些替代方案。所以,有一个解决方法:您可以禁用地图的工具栏。并且可以在布局中的任何位置添加 appcompact 的工具栏。
参考:https://developer.android.com/reference/android/support/v7/widget/Toolbar.html
这是我之前遇到的关于如何将工具栏移动到页面底部的答案。希望对你有帮助。
//get reference to my location icon
View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("2"));
// and next place it, for example, 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);
编辑: 根据(@Andro 的)建议,这是更改地图工具栏位置的代码:
//获取对我的位置图标的引用
View toolbar = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
getParent()).findViewById(Integer.parseInt("4"));
// and next place it, for example, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.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);
2021 年更新
您应该能够通过设置 googleMap 对象上的填充来移动地图 UI 控件(罗盘、我的位置、缩放等)。这是我发现的唯一提供的内部方式。这是来自文档的信息:
Padding can be helpful when designing UIs that overlap some portion of the map. For example, in the below image, the map is padded along the top and right edges. Visible map controls and legal text will be displayed along the edges of the padded region
https://developers.google.com/maps/documentation/android-sdk/map#map_padding