Android 在 MapView 中放置缩放控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/263507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing Zoom Controls in a MapView
提问by jasonhudgins
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?
我试图让缩放控件显示在 a 中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);
These views/layouts are all constructed programmatically, there is no layout file to tweak.
这些视图/布局都是以编程方式构建的,无需调整布局文件。
回答by jcrowson
Add the following line to the OnCreate()
method of your MapView
Class:
将以下行添加到OnCreate()
您的MapView
类的方法中:
view.setBuiltInZoomControls(true);
view.setBuiltInZoomControls(true);
回答by Reto Meier
The trick here is to place another Layout container where you want to put the ZoomControls and then insert the ZoomControls into that.
这里的技巧是在要放置 ZoomControl 的位置放置另一个 Layout 容器,然后将 ZoomControl 插入其中。
The real trick is to use the RelativeLayout
rather than LinearLayout
to position the elements, as shown in this sample layout.xml
:
真正的技巧是使用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>
The layout_zoomLinearLayout element is positioned in the bottom center of the screen, placing it over the middle/bottom of the MapView
.
所述layout_zoom的LinearLayout元件被定位在屏幕的底部中心,将其放置在的中间/底部MapView
。
Then within your Activity's onCreate
, get a reference to the layout_zoomelement and insert the ZoomControl into it, much like you've already done:
然后在您的 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);
The ZoomControls should now appear on a long click, without stealing the map touch events.
ZoomControls 现在应该在长按时出现,而不会窃取地图触摸事件。
回答by George
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;
}
}
回答by jasonhudgins
Reto : thanks for your reply, but the idea was to do it withoutusing XML layouts.
Reto :感谢您的回复,但我们的想法是不使用 XML 布局。
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).
我最终解决了这个问题。因为 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);
回答by jasonhudgins
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:
不幸的是,我无法在 11 月 28 日 6:32 对 Jason Hudgins 批准的解决方案添加评论,但他的代码出现了一个小错误:
In this line:
在这一行:
MapView.LayoutParams params = MapView.LayoutParams(
The error Eclipse gave me was
Eclipse 给我的错误是
"The method LayoutParams(int, int, int, int, int) is undefined for the type MapView"
“未定义 MapView 类型的 LayoutParams(int, int, int, int, int) 方法”
instead, creating a new MapView.LayoutParams object fixed it, like this:
相反,创建一个新的 MapView.LayoutParams 对象来修复它,如下所示:
MapView.LayoutParams params = **new** MapView.LayoutParams(
It took me some time to find out, as I am a n00b :D
我花了一些时间才知道,因为我是 n00b :D
回答by magegu
from the google groups threadi found this:
从谷歌组线程我发现这个:
ZoomControls without XML:
没有 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
使用 SDK1.1 为我工作 100%
回答by Manoj Reddy
You can try this:
你可以试试这个:
MapView map = (MapView)findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
回答by Manoj Reddy
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?
Reto - 使用 FILL_PARENT 的问题是缩放控件然后“窃取”所有触摸事件;这样您就无法在缩放控件可见时平移地图。你知道如何防止这种情况吗?