改变谷歌地图的位置SupportMapFragment Zoom和我的位置控件不起作用 [英] changing the position of google maps SupportMapFragment Zoom and My location controls not work
问题描述
我试图改变放大率和放大率我的位置控制着Android谷歌地图的位置。
我想在右下方设置缩放控制位置,并将我的位置位置设置在 MapFragment
(我正在使用 com.google.android.gms.maps.SupportMapFragment
)。
这是我的地图片段布局:
< fragment
android:id =@ + id / map
android:layout_below =@ + id / toolbar
class =com.google.android.gms.maps.SupportMapFragment
android:layout_width =match_parent
android:layout_height =match_parent/>
改变控件位置的编写代码( getMapAsync
回调):
pre $ public c onMapReady(GoogleMap googleMap){
map = googleMap;
if(map!= null){
// Map is ready
map.getUiSettings()。setZoomControlsEnabled(true);
查看mapView = mapFragment.getView();
//缩放控制
@SuppressLint(ResourceType)查看zoomButton = mapView.findViewById(0x1);
if(zoomButton!= null&&&& zoomButton.getLayoutParams()instanceof RelativeLayout.LayoutParams){
RelativeLayout.LayoutParams zp =(RelativeLayout.LayoutParams)zoomButton.getLayoutParams ();
zp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
zp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
zp.setMargins(0,0,30,30);
}
//我的位置控制
@SuppressLint(ResourceType)查看locationButton = mapView.findViewById(0x2);
if(locationButton!= null&& locationButton.getLayoutParams()instanceof RelativeLayout.LayoutParams){
RelativeLayout.LayoutParams lp =(RelativeLayout.LayoutParams)locationButton.getLayoutParams ();
//在左下方的位置
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(30,0,0,30);
}
}
但它不起作用,我想知道我的代码有什么问题。有没有更好的解决方案呢?
[更新 - 1]
我的应用程序语言是波斯语(FA),所以我在 AndroidManifest.xml中添加了
文件,以支持RTL语言。 android:supportsRtl =true
问题:缩放控件对齐左下角,我将其重新放置在右下角。 / strong>
您可以通过先获取父视图然后按下我的位置按钮来在地图上找到位置按钮:
//获取我的位置按钮视图
查看locationButton =((View)mapView.findViewById(1 ).getParent())findViewById(2)。
//获取按钮参数以放置按钮
RelativeLayout.LayoutParams lp =(RelativeLayout.LayoutParams)locationButton.getLayoutParams();
//放置在右下角
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
rlp.setMargins(0,0,30,30);
如果想要调整按钮大小,您可以获取如下参数:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100,100);
// ....执行编辑
//将参数设置为按钮
locationButton.setLayoutParams(lp);
I'm trying to change the zoom & my location controls positions in Android Google maps.
I want to set zoom control position at the right bottom and set my location position at the left bottom of MapFragment
(I'm using the com.google.android.gms.maps.SupportMapFragment
).
Here is my map fragment layout:
<fragment
android:id="@+id/map"
android:layout_below="@+id/toolbar"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The written codes for changing the controls position (getMapAsync
callback):
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (map != null) {
// Map is ready
map.getUiSettings().setZoomControlsEnabled(true);
View mapView = mapFragment.getView();
// Zoom control
@SuppressLint("ResourceType") View zoomButton = mapView.findViewById(0x1);
if (zoomButton != null && zoomButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams zp = (RelativeLayout.LayoutParams) zoomButton.getLayoutParams();
zp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
zp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
zp.setMargins(0, 0, 30, 30);
}
// My location control
@SuppressLint("ResourceType") View locationButton = mapView.findViewById(0x2);
if (locationButton != null && locationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on left bottom
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(30, 0, 0, 30);
}
}
But it doesn't work and I'd like to know what's wrong with my code. Is there a better solution for doing that?
[Updated - 1]
My application language is Persian (FA) so I've added android:supportsRtl="true"
in the AndroidManifest.xml
file in order to support RTL language.
Problem: The zoom control is aligned at the bottom left which I would reposition it at bottom right corner.
You can find Location button on map by getting parent view first and then "My Location" button as below :
// Get the "My Location" button view
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);
// get button params to place the button
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// place on right bottom corner
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
If want to resize button, you can get parameters like below :
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 100);
//.... do your editing
// To set params to button
locationButton.setLayoutParams(lp);
这篇关于改变谷歌地图的位置SupportMapFragment Zoom和我的位置控件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!