【问题标题】:How to check if an Google Maps InfoWindow is still displayed before updating it?如何在更新之前检查 Google Maps InfoWindow 是否仍然显示?
【发布时间】:2012-11-24 14:41:20
【问题描述】:

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

现在 Maps API 声明如下:

请注意,将拍摄返回视图的快照,然后在地图上呈现,因此对视图的后续更改不会反映在地图上的信息窗口中。要更新信息窗口(例如,加载图像后),只需调用 showInfoWindow() 即可更新视图。

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

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

有没有办法只在信息窗口仍然显示时更新它?

【问题讨论】:

  • 你有没有解决这个问题?我有同样的问题!!任何帮助表示赞赏!
  • 作为一名专业的 Android 开发者,我猜你找到了解决方案? :) 在你的博客上吗?因为我在那里找不到任何东西:(

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


【解决方案1】:

我刚刚在AsyncTasks 下载和更新InfoWindow 时遇到了类似的问题,今天早上我把头撞在墙上后,我想出了这个小解决方法,希望能满足您的需求直到谷歌解决这个问题。

我在我的AsyncTask 的OnPostExecute() 方法中调用marker.showInfoWindow(),它重新调用了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;
            }
        });

以及来自DownloadBubbleInfoAsyncTask的相关行:

    @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 的另一个角落!

【讨论】:

    【解决方案2】:

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

    Marker#isInfoWindowShown() - Google Maps Android API v2 documentation

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

    How to get Latitude/Longitude span in Google Map V2 for Android

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

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2014-01-28
      • 2015-12-01
      相关资源
      最近更新 更多