问题
I have a problem with marker on google maps api v2. I would like to customize infoWindows with a WebView:
Here my code of 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;
}
});
And this is the layout:
<?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>
My problem is that I see the content into the TextView, but not the content into the WebView. What i'm doing wrong? Thanks a lot
回答1:
You can't use a WebView directly. The reason is (source: official docs):
Note: The info window that is drawn is not a live view. The view is rendered as an image (using
View.draw(Canvas)
) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), callshowInfoWindow()
. Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
You can try drawing the webview on a bitmap (after the content has been loaded) and supplying an ImageView
with that bitmap in the InfoWindow
adapter, but you will lose interactivity with the webview anyway.
回答2:
after a lot of tests, googling and some reversing I can say that view you return in getInfoContents
is used to render into a Bitmap
and then hidden. The bitmap
is then displayed via GL
I've found a workaround that can be fine for you.
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;
}
});
}
in this example I've used the snippet to store the url, It's not exactly what did you asked, but it may look similar.
Edit: the first version was recycling the WebView
but it's not possible without some trick to remove it from the Alert
, this revised version doesn't shows the InfoContents
box below but has still an issue, when pressed it keeps a sort of state, and it believes the marker is clicked after closing the dialog.
You may use a layout that contains a WebView
to decor a bit the window
Edit2: Reverted to something like the first version, a blank InfoContents
box shows for bit I'm not sure it's avoidable.
来源:https://stackoverflow.com/questions/15989891/android-setinfowindowadapter-with-webview-in-google-marker-api-v2