【问题标题】:Android - setInfoWindowAdapter with WebView in google Marker (api v2)Android - 在 google Marker (api v2) 中使用 WebView 的 setInfoWindowAdapter
【发布时间】:2013-04-06 01:47:32
【问题描述】:

我在 google maps api v2 上遇到了标记问题。 我想用 WebView 自定义 infoWindows:

这是我的 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 中没有看到内容。 我做错了什么? 非常感谢

【问题讨论】:

    标签: android webview google-maps-markers google-maps-api-2


    【解决方案1】:

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

    注意:绘制的信息窗口不是实时视图。视图在返回时呈现为图像(使用View.draw(Canvas))。这意味着对视图的任何后续更改都不会反映在地图上的信息窗口中。要稍后更新信息窗口(例如,加载图像后),请致电 showInfoWindow()。此外,信息窗口不会尊重任何普通视图的典型交互性,例如触摸或手势事件。但是,您可以在整个信息窗口上收听通用点击事件,如下节所述。

    您可以尝试在位图上绘制 webview(在加载内容之后)并在 InfoWindow 适配器中提供带有该位图的 ImageView,但无论如何您都会失去与 webview 的交互性。

    【讨论】:

    【解决方案2】:

    经过大量测试、谷歌搜索和一些反转后,我可以说您在getInfoContents 返回的视图用于渲染为Bitmap,然后隐藏。然后通过GL 显示bitmap

    我找到了适合您的解决方法。

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

    在这个例子中,我使用了 sn-p 来存储 url,这不是 完全你问的,但它可能看起来相似。

    编辑:第一个版本是回收WebView,但如果没有一些技巧就无法将它从Alert 中删除,这个修订版不会显示下面的InfoContents 框但是还有一个问题,当按下它时它会保持一种状态,并且它认为在关闭对话框后单击了标记。

    您可以使用包含WebView 的布局来装饰窗口

    Edit2:恢复到类似于第一个版本的内容,一个空白的InfoContents 框显示有点我不确定它是否可以避免。

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多