2013-04-13 73 views
1

我在google maps api v2上有標記問題。 我想自定義信息窗口具有的WebView:Android - 帶有WebView的setInfoWindowAdapter在google標記(api v2)

這裏我InfoWindowAdapter

mMap.setInfoWindowAdapter(new InfoWindowAdapter() { 
     @Override 
     public View getInfoWindow(Marker arg0) { 
      return null; 
     } 
     @Override 
     public View getInfoContents(Marker arg0) { 
      View v = getLayoutInflater().inflate(R.layout.info_window_layout, null); 
      TextView title = (TextView) v.findViewById(R.id.title_marker); 
      WebView snippet = (WebView) v.findViewById(R.id.item_snippet); 
      title.setText(arg0.getTitle()); 
     snippet.setVisibility(WebView.VISIBLE); 
     snippet.loadData(arg0.getSnippet(),"text/html", "UTF-8"); 
      return v; 

     } 
    }); 

的代碼是這樣的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/title_marker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <WebView 
     android:id="@+id/item_snippet" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

我的問題是,我看到的內容進入TextView,但不包含到WebView中的內容。 我做錯了什麼? 非常感謝

回答

2

您不能直接使用WebView。原因是(來源:官方文檔):

注意:繪製的信息窗口不是實時視圖。該視圖在返回時呈現爲圖像(使用View.draw(Canvas))。這意味着對視圖的任何後續更改都不會反映在地圖上的信息窗口中。稍後更新信息窗口(例如,在圖像載入後),請致電showInfoWindow()。此外,信息窗口不會考慮任何典型的普通視圖的交互性,例如觸摸或手勢事件。但是,您可以在整個信息窗口中收聽通用點擊事件,如下面的部分所述。

你可以嘗試繪製的WebView上的位圖(內容已被加載後),並在InfoWindow適配器位供給ImageView,但你會失去交互性的網頁流量反正。

+0

感謝您的回答。我試過[鏈接] http://stackoverflow.com/questions/6747701/how-to-convert-a-webview-into-an-imageview中的代碼,但它返回一個空白圖像。我不知道爲什麼。你能幫我嗎?再次感謝 – user2277798

1

經過大量的測試,谷歌搜索和一些倒退我可以說,您在getInfoContents返回的視圖用於渲染成Bitmap,然後隱藏。 bitmap然後通過GL

顯示我找到了一個解決方法,可以爲你罰款。

private View mGhost; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mGhost = new View(this); 
    mGhost.setLayoutParams(new LayoutParams(0, 0)); 
    mGhost.setVisibility(View.GONE);   

    .... 
} 

public void onMapReady(GoogleMap map) // or whatever 
{ 
    map.setInfoWindowAdapter(new InfoWindowAdapter() 
    { 
     @Override 
     public View getInfoWindow(Marker marker) 
     { 
      return null; 
     } 

     @Override 
     public View getInfoContents(final Marker marker) 
     { 
      WebView webview = new WebView(MainActivity.this); 
      webview.loadUrl(marker.getSnippet()); 
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      AlertDialog alert = builder.setView(webview).create(); 
      alert.show(); 
      alert.getWindow().setLayout(200, 200); 
      mGhost.post(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        marker.hideInfoWindow(); 
       } 
      }); 
      return mGhost; 
     } 
    }); 
} 

在這個例子中,我使用的片段存儲網址,這不正是什麼做你問,但它可能看起來相似。

編輯:第一個版本被回收WebView但它不可能沒有一些技巧,從Alert刪除它,這個修改後的版本不顯示下列InfoContents盒,但是當按下它不斷有仍然是一個問題,一種狀態,它相信在關閉對話框後點擊標記。

您可以使用包含WebView來裝飾了一下窗口

EDIT2佈局:恢復到像第一個版本,一個空白InfoContents框顯示了位我不知道它是可以避免的。

相關問題