19

I'm trying to get the zoom controls to show up in a mapview, the following code almost works, but the zoom controls appear in the top left of the mapview, not the bottom center like I'm specifying via setGravity(). Can someone enlighten me as to what I'm missing?

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

These views/layouts are all constructed programmatically, there is no layout file to tweak.

4

8 回答 8

23

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

view.setBuiltInZoomControls(true);

于 2010-03-23T12:41:57.843 回答
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 现在应该出现在长按上,而不会窃取地图触摸事件。

于 2008-11-11T07:27:39.023 回答
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;
    } 
}
于 2011-05-12T22:51:38.790 回答
4

Reto:感谢您的回复,但我们的想法是使用 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);
于 2008-11-28T06:32:53.580 回答
2

不幸的是,我无法从 11 月 28 日 6:32 开始对 Jason Hudgins 批准的解决方案添加评论,但我的代码出现了一个小错误:

在这一行:

MapView.LayoutParams params = MapView.LayoutParams(

Eclipse 给我的错误是

“方法 LayoutParams(int, int, int, int, int) 对于 MapView 类型未定义”

相反,创建一个新的 MapView.LayoutParams 对象来修复它,如下所示:

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

我花了一些时间才知道,因为我是 n00b :D

于 2009-01-22T22:44:53.670 回答
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 为我工作 100%

于 2009-02-27T11:45:14.717 回答
2

你可以试试这个:

MapView map = (MapView)findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
于 2018-09-25T06:59:22.593 回答
0

Reto - 使用 FILL_PARENT 的问题是缩放控件会“窃取”所有的触摸事件;这样您就无法在缩放控件可见时平移地图。你知道如何防止这种情况吗?

于 2008-11-08T20:24:37.723 回答