在地图视图中放置缩放控件

发布于 2024-07-08 13:06:30 字数 478 浏览 6 评论 0原文

我试图让缩放控件显示在 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);

这些视图/布局都是以编程方式构建的,没有需要调整的布局文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

客…行舟 2024-07-15 13:06:30

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

view.setBuiltInZoomControls(true);

Add the following line to the OnCreate() method of your MapView Class:

view.setBuiltInZoomControls(true);

泛泛之交 2024-07-15 13:06:30

这里的技巧是在要放置 ZoomControls 的位置放置另一个布局容器,然后将 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 现在应该出现在长条上单击,而不窃取地图触摸事件。

The trick here is to place another Layout container where you want to put the ZoomControls and then insert the ZoomControls into that.

The real trick is to use the RelativeLayout rather than LinearLayout to position the elements, as shown in this sample 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> 

The layout_zoom LinearLayout element is positioned in the bottom center of the screen, placing it over the middle/bottom of the MapView.

Then within your Activity's onCreate, get a reference to the layout_zoom element and insert the ZoomControl into it, much like you've already done:

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

The ZoomControls should now appear on a long click, without stealing the map touch events.

岁月静好 2024-07-15 13:06:30

上面的内容对我不起作用,但这样做(将控件放在右下角):

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

The above didn't work for me, but this does (to place the control on the bottom right):

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;
    } 
}
风筝有风,海豚有海 2024-07-15 13:06:30

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

Reto : thanks for your reply, but the idea was to do it without using XML layouts.

I eventually worked out the problem. Because a MapView is a subclass of ViewGroup, you can easily add child views (like the zoom controls). All you need is a MapView.LayoutParams instance and you're good to go. I did something like this (puts zoom controls in the bottom center of the mapview).

  // 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);
2024-07-15 13:06:30

google groups thread我发现了这个:

ZoomControls without XML :

   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% 有效

from the google groups thread i found this:

ZoomControls without XML:

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

 } 

was 100% working for me with SDK1.1

波浪屿的海角声 2024-07-15 13:06:30

不幸的是,我无法从 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

Unfortunately I cant add a comment to Jason Hudgins approved solution from Nov 28 at 6:32 but I got a tiny error with his code:

In this line:

MapView.LayoutParams params = MapView.LayoutParams(

The error Eclipse gave me was

"The method LayoutParams(int, int,
int, int, int) is undefined for the
type MapView"

instead, creating a new MapView.LayoutParams object fixed it, like this:

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

It took me some time to find out, as I am a n00b :D

他不在意 2024-07-15 13:06:30

你可以试试这个:

MapView map = (MapView)findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);

You can try this:

MapView map = (MapView)findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
梦与时光遇 2024-07-15 13:06:30

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

Reto - the problem with using FILL_PARENT is that the zoom control then "steals" all of the touch events; so that you can't pan the map while the zoom controls are visible. Do you know how to prevent this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文