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

前端之家收集整理的这篇文章主要介绍了android – 如何在更新之前检查Google Maps InfoWindow是否仍然显示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开始研究在InfoWindow中显示位置图像的InfoWindowAdapter.我有很多不同的位置,因此有可能手机上没有位置图像,而是从网上下载.

现在Maps API声明了以下内容

Note that a snapshot of the returned view will be taken and then rendered on the map so subsequent changes to the view will not be reflected by the info window on the map. To update the info window (e.g.,after an image has loaded),just call showInfoWindow() and the view will be updated.

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

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

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

解决方法

我刚刚遇到类似AsyncTasks下载和更新InfoWindow的问题,并且在今天早上我的头撞到墙上之后,我已经想出了这个小小的解决方法,希望能满足你的需求,直到谷歌排除这一点.

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

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

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

以及来自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");
}

现在,所有这一切都应该确保正确的标记正在使用从AsyncTask返回的正确信息进行填充,并且他们已经成功环绕了极其笨拙的GoogleMaps v2 for Android API的另一个角落!

猜你在找的Android相关文章