如何在更新之前检查Google Maps InfoWindow是否仍然显示?

Jan*_*usz 6 android google-maps google-maps-android-api-2

我开始研究在InfoWindow中显示位置图像的InfoWindowAdapter.我有很多不同的位置,因此有可能手机上没有位置图像,而是从网上下载.

现在Maps API声明了以下内容:

请注意,将返回返回视图的快照,然后在地图上呈现,以便后续对视图的更改不会被地图上的信息窗口反映出来.要更新信息窗口(例如,在加载图像之后),只需调用showInfoWindow()并更新视图.

据我所知,我需要跟踪创建我想要更新的信息窗口的标记,并在加载图像后调用showInfoWindow.我有一个ImageDownloadService,它将采用一个处理程序,如果图像加载完成则通知Handler.

如果图像加载时间稍长,则用户可能会打开另一个InfoWindow或关闭当前窗口滚动.问题是,当我再次在标记上调用showInfoWindow来更新视图时,我无法确定InfoWindow是否仍然显示.

有没有办法只在它仍然显示的情况下更新InfoWindow?

小智 6

我刚刚在AsyncTask下载和更新时遇到了类似的问题,InfoWindow并且在今天早上我的头撞到了墙上之后,我已经想出了这个小小的解决方法,希望能满足你的需求,直到Google对此进行排序.

我正在调用my marker.showInfoWindow()OnPostExecute()方法AsyncTask,它重新调用了InfoWindowAdapter方法,这些方法最终在一个循环中,并且从未正确地传播我的更改.

我使用的解决方案是存储当前选择的标记和InfoWindow我想要显示的视图.我已经在下面的例子中看到了TextView更新的内容DownloadBubbleInfo AsyncTask(类似于我认为的图像线程).

            // Setting a custom info window adapter for the google map
        gMap.setInfoWindowAdapter(new InfoWindowAdapter() {

            // Use default InfoWindow frame
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            public View getInfoContents(Marker arg0) {
                if (selectedMarker.isInfoWindowShown()) {
                    return infoWindowView;
                } else {
                    // Getting view from the layout file info_window_layout
                    infoWindowView = getLayoutInflater().inflate(
                            R.layout.bubblewindowlayout, null);
                    // Stash the base view in infoWindowView
                    // Getting reference to the TextView to set latitude
                    TextView tvTit = (TextView) infoWindowView
                            .findViewById(R.id.tv_title);
                    tvTit.setText("Fetching data...");

                    // Async the update so we're not slowed down waiting for
                    // the
                    // bubble to populate
                    new DownloadBubbleInfo(context, infoWindowView, arg0)
                            .execute(arg0.getTitle(), arg0.getSnippet());

                    // Returning the view containing InfoWindow contents
                    return infoWindowView;
                }
            }
        });
        gMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            public boolean onMarkerClick(Marker marker) {
                // When a marker is clicked set it as the selected marker so
                // we can track it for the InfoWindow adapter. This will
                // make sure that the correct marker is still displayed when
                // the callback from DownloadBubbleInfo is made to
                // marker.showInfoWindow() which is needed to update the
                // InfoWindow view.
                selectedMarker = marker;
                infoWindowView = null;
                return false;
            }
        });
Run Code Online (Sandbox Code Playgroud)

而相关的线路来自DownloadBubbleInfo AsyncTask:

    @Override
protected String[] doInBackground(String... queryparts) {
    // Do the query and stash the results in queryResults and pass to
    // onPostExecute to attach to the mainview (the current view from the
    // main code) and then call showInfoWindow on the marker to re-launch
    // the InfoWindowAdapter methods again to repopulate the InfoWindow view
    // and attach it.
    return queryResults;
}
protected void onPostExecute(String[] results) {
    ((TextView) mainview.findViewById(R.id.tv_title)).setText(results[0]);
    ((TextView) mainview.findViewById(R.id.tv_info)).setText(results[1]);

    marker.showInfoWindow();
    Log.i("Chris-Debug", "Reshowing InfoWindow");
}
Run Code Online (Sandbox Code Playgroud)

现在,所有这一切应该确保正确的标记正在填充正确的信息从您AsyncTask和嘿presto非常笨拙的GoogleMaps v2 for Android API的另一个角落成功环游!


Art*_*miy 3

Marker 的布尔 isInfoWindowShown() 方法可能是您正在寻找的方法吗?

Marker#isInfoWindowShown() - Google 地图 Android API v2 文档

但是,当 InfoWindow 滚动离开时,它无济于事,为此,我认为您可能必须将标记的 LatLng 坐标转换为屏幕坐标,或使用它来检查标记是否仍在屏幕上:

如何在 Android 版 Google 地图 V2 中获取纬度/经度范围

我不确定是否可以检查窗口本身是否仍在地图视图范围内。