2008-11-04 65 views
17

我試圖讓縮放控件顯示在mapview中,下面的代碼幾乎可以工作,但縮放控件出現在mapview的左上角,而不是像I這樣的底部中心'通過setGravity()指定。有人能告訴我我錯過了什麼嗎?將縮放控件放置在MapView中

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

這些視圖/佈局都是以編程方式構建的,沒有佈局文件可以調整。

回答

0

Reto - 使用FILL_PARENT的問題是縮放控件「竊取」所有觸摸事件;以便在縮放控件可見時無法平移地圖。你知道如何防止這種情況嗎?

+0

你是對的。沒有注意到這一點。用更好的解決方案取代了以前的答案,避免了這個問題。 – 2008-11-11 07:28:15

19

這裏的訣竅是放置另一個佈局容器,放置ZoomControls,然後在其中插入ZoomControl。

真正的竅門是使用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的中間/底部。

中的活動的 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現在應該出現在長按,而不會竊取地圖觸摸事件。

+0

你好,我可以編輯你的答案,在WebView上添加實現嗎?只是一對4-5線。我使用谷歌並找到了這個解決方案。但是,要在WebView上使用它,我需要修改一下。 – ariefbayu 2011-03-14 06:19:14

4

Reto:感謝您的回覆,但我們的想法是使用XML佈局來做而不使用

我最終解決了這個問題。由於MapView是ViewGroup的子類,因此可以輕鬆添加子視圖(如縮放控件)。所有你需要的是一個MapView.LayoutParams實例,你很好去。我做了這樣的事情(將縮放控件放在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); 
2

不幸的是,我不能添加評論,傑森 - 哈金斯批准從11月28日溶液中6:32,但我有一個微小的錯誤與他的代碼:

在這一行:

MapView.LayoutParams params = MapView.LayoutParams(

Eclipse給我的錯誤是

「方法LayoutParams(int,int, int,int,int)未定義爲th e type MapView「

改爲創建一個新的MapView。的LayoutParams對象固定的,就像這樣:

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

我花了一些時間來找到答案,因爲我是一個的n00b:d

2

google groups thread我發現這一點:

ZoomControls沒有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); 

} 

是100%,我正與SDK1.1

23

以下行添加到您的MapView類的OnCreate()方法:

view.setBuiltInZoomControls(true);

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

最後。沒有調用已棄用的getZoomControls()方法的答案。榮譽給你。謝謝。 – 2012-02-12 05:36:43