在MapView中放置缩放控件

19

我正在尝试在mapview中显示缩放控件,以下代码几乎有效,但是缩放控件出现在mapview的左上角,而不像我通过setGravity()指定的底部中心。 有人能告诉我我错过了什么吗?

zoomView = (LinearLayout) mapView.getZoomControls();
zoomView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); 
zoomView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
mapView.addView(zoomView);

这些视图/布局都是通过编程构建的,没有布局文件可以调整。

8个回答

23

将以下代码添加到您的MapView类的OnCreate()方法中:

view.setBuiltInZoomControls(true);

该行代码可以使地图视图具备内置缩放控件的功能。

19

其中的技巧是在想要放置ZoomControls的位置上放置另一个Layout容器, 然后将ZoomControls插入该容器中。

真正的技巧是使用RelativeLayout而不是LinearLayout来定位元素,如此示例layout.xml所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
  <com.google.android.maps.MapView
    android:id="@+id/myMapView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="MY_MAP_API_KEY"
  />
  <LinearLayout android:id="@+id/layout_zoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
  /> 
</RelativeLayout> 

layout_zoom LinearLayout元素位于屏幕底部中心,将其放置在MapView的中下部。

然后在Activity的onCreate中,获取对layout_zoom元素的引用,并将ZoomControl插入其中,就像您已经做的那样:

LinearLayout zoomLayout =(LinearLayout)findViewById(R.id.layout_zoom);  
View zoomView = myMapView.getZoomControls(); 
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
myMapView.displayZoomControls(true);

现在,在长按操作时,ZoomControls 应该显示出来,而不会夺取地图的触摸事件。


你好,我能否编辑你的答案,在WebView上添加一个实现?只需要4-5行。我使用谷歌找到了这个解决方案。但是,为了在WebView上使用它,我需要稍作修改。 - ariefbayu

7
上述方法对我无效,但以下方法有效(将控件放在右下角):
mapView.setBuiltInZoomControls(true);
ZoomButtonsController zbc = mapView.getZoomButtonsController();
ViewGroup container = zbc.getContainer();
for (int i = 0; i < container.getChildCount(); i++) {
    View child = container.getChildAt(i);
    if (child instanceof ZoomControls) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
        lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
        child.requestLayout();
        break;
    } 
}

终于,一个不需要调用已弃用的getZoomControls()方法就能工作的答案。向你致敬,谢谢。 - Michael Reed
太好了,这是一个更好的解决方案,它允许您保留内置的缩放控件,并完整地实现淡入/淡出效果。谢谢! - Chris Knight

4

您好,感谢您的回复。但是我的想法是不使用XML布局来实现。

我最终解决了这个问题。因为MapView是ViewGroup子类,您可以轻松地添加子视图(例如缩放控件)。您只需要一个MapView.LayoutParams实例即可。我做了类似以下的事情(将缩放控件放在地图视图的底部中心位置)。

  // layout to insert zoomcontrols at the bottom center of a mapview
  MapView.LayoutParams params = MapView.LayoutParams(
    LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT,
    mapViewWidth / 2, mapViewHeight,
    MapView.LayoutParams.BOTTOM_CENTER);

  // add zoom controls
  mapView.addView(mapView.getZoomControls(), params);

2
你可以尝试这个方法:
MapView map = (MapView)findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);

2
很抱歉,我无法在Jason Hudgins于11月28日6:32批准的解决方案中添加评论,但是我发现他的代码有一个小错误:

在这一行中:

MapView.LayoutParams params = MapView.LayoutParams(

我收到的错误信息是:

"方法 LayoutParams(int, int, int, int, int) 在类型 MapView 中未定义"

相反,创建一个新的 MapView.LayoutParams 对象可以解决这个问题,像这样:

MapView.LayoutParams params = **new** MapView.LayoutParams(

我花了一些时间才找到答案,因为我是一个新手 :D


2

我在谷歌群组讨论帖中找到了以下内容:

不使用XML创建ZoomControls:

   public class MyMapActivity extends MapActivity {  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout relativeLayout = new RelativeLayout(this);
        setContentView(relativeLayout);

        final MapView mapView = new MapView(this, DEBUG_MAP_API_KEY);
        RelativeLayout.LayoutParams mapViewLayoutParams = new
RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT );
        relativeLayout.addView(mapView, mapViewLayoutParams);

        RelativeLayout.LayoutParams zoomControlsLayoutParams = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT );
        zoomControlsLayoutParams.addRule
(RelativeLayout.ALIGN_PARENT_BOTTOM);
        zoomControlsLayoutParams.addRule
(RelativeLayout.CENTER_HORIZONTAL);

        relativeLayout.addView(mapView.getZoomControls(),
zoomControlsLayoutParams);

        mapView.setClickable(true);
        mapView.setEnabled(true);

 } 

在使用SDK1.1时,这对我来说是百分之百有效的。


0
Reto - 使用FILL_PARENT的问题在于缩放控件会“窃取”所有触摸事件,因此当缩放控件可见时,您无法平移地图。您知道如何防止这种情况发生吗?

你说得对。我没有注意到这一点。用一个更好的解决方案替换了之前的答案,避免了这个问题。 - Reto Meier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接