【问题标题】:Android add dynamics data to InfoWindowAndroid向InfoWindow添加动态数据
【发布时间】:2014-10-18 16:53:44
【问题描述】:

我正在使用 Google Map 和 Google Places API 开发一个应用程序,假设我使用不同的标记填充地图,并且我在 a 中跟踪这些标记

Map<Marker, Place> places = new HashMap<Marker, Place>();

这是我的地方的班级:

    public class Place {
    String placeId;
    String name;

    public Place(String placeId, String name) {
        this.placeId = placeId;
        this.name = name;
    }
}

我希望能够使用基于 placeId 参数获取的数据动态填充 InfoWindow,这是我在 InfoWindowAdapter 中所做的:

map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
                TextView placeName = (TextView)v.findViewById(R.id.info_window_place_name);
                Place place = places.get(marker);
                if (place != null) {
                    placeName.setText(place.name);
                    String photoUrl = "http://www.plopcontenido.com/wp/wp-content/themes/PlopTheme/img/logo.png";
                    new DownloadPlacePhoto(v, marker, placeName.toString()).execute(photoUrl);
                }
                return v;
            }
        });

    private class DownloadPlacePhoto extends AsyncTask<String, Void, Bitmap> {
    View v;
    Marker marker;
    String placeName;

    public DownloadPlacePhoto(View v, Marker marker, String placeName) {
        this.v = v;
        this.marker = marker;
        this.placeName = placeName;
    }

    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap download;
        try {
            InputStream in = new URL(urls[0]).openStream();
            download = BitmapFactory.decodeStream(in);
            return download;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap download) {
        if (download != null) {
            ImageView placeImage = (ImageView)v.findViewById(R.id.info_window_place_photo);
            placeImage.setImageBitmap(download);
            placeImage.setContentDescription(this.placeName);
            placeImage.setVisibility(View.VISIBLE);
            if (this.marker.isInfoWindowShown()) {
                this.marker.hideInfoWindow();
                this.marker.showInfoWindow();
            }
        }
    }
}

问题是 InfoWindow 是一个“快照”而不是实时表示(我完全理解为什么)是关联的 Asynctask 将在另一个线程中运行,因此在没有我获取的数据的情况下已经拍摄了快照。

我听到人们谈论观察者模式和其他人谈论“您需要在进入 getInfoWindow 函数之前存储您的数据,但由于 Google Places 的限制,我无法再执行两个请求(一个用于图片,另一个用于获取有关特定地点的更多数据)用于每个标记。

知道如何执行此操作吗?

【问题讨论】:

  • 这可能会有所帮助,它是关于在创建后将图像添加到 InfoWindow,但它基本上是相同的问题:stackoverflow.com/questions/15503266/…
  • 我已经看过这个帖子了,但我不是真正的Android大师,还在努力学习,作者对我来说有点太宽泛了。

标签: android google-maps google-places-api infowindow


【解决方案1】:

this question & answer 的要点是当您拥有所需的所有数据时,用marker.showInfoWindow() 重新显示InfoWindow,在此之前您可以显示您已经拥有的数据或一些“正在加载...”-文本。 这样做时,您必须检查用户是否同时取消选择了相应的标记或选择了另一个标记。

【讨论】:

  • 我尝试这样做(并将更新我的代码以显示它)但我只是让 InfoWindow 每 3 或 4 秒刷新一次(没有显示图片)
猜你喜欢
  • 2011-06-02
  • 2016-08-07
  • 2013-06-10
  • 1970-01-01
  • 2020-10-23
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多