【问题标题】:Calling showInfoWindow() for second time does not update the info window第二次调用 showInfoWindow() 不会更新信息窗口
【发布时间】:2015-07-28 09:06:01
【问题描述】:

事情是这样的: 我有一张带有标记的地图(由 ClusterManager 管理),每次单击它们时都会显示一个信息窗口。我还在他们被点击的时候获取他们的地址,所以一旦我得到它,我会再次在标记上调用 showInfoWindow() 以更新它。问题是地址不会显示在信息窗口中。这是我的 InfoWindowAdapter(根据单击的标记类型,我有两个视图):

class CabinetInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    public CabinetInfoWindowAdapter () {

    }

    @Override
    public View getInfoWindow(Marker marker) {
        // Use the default info window frame
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        Cabinet cabinet = clickedCabinetMarker.getCabinet();
        Log.i("fttxgr", "info contents, cabinet type: " + cabinet.getType().toString());

        switch (cabinet.getType()) {
            case ADSL:
            case VDSL:
                return getCabinetView(cabinet, marker);

            case DSLAM:
                return getDslamView(cabinet, marker);

            default: return null;
        }
    }

    private final View getCabinetView (final Cabinet cabinet, final Marker marker) {
        View view = getLayoutInflater().inflate(R.layout.cabinet_info_window, null);

        TextView cabinetId = (TextView) view.findViewById(R.id.cabinet_id);
        TextView cabinetType = (TextView) view.findViewById(R.id.cabinet_type);
        TextView cabinetAddress = (TextView) view.findViewById(R.id.cabinet_address);
        TextView cabinetCoordinates = (TextView) view.findViewById(R.id.cabinet_coordinates);
        TextView cabinetUserNick = (TextView) view.findViewById(R.id.cabinet_user_nick);
        ImageView cabinetImage = (ImageView) view.findViewById(R.id.cabinet_image);
        View header = view.findViewById(R.id.header);

        cabinetId.setText (cabinet.getId() + " - " +  cabinet.getCabinetNumber());
        cabinetType.setText (cabinet.getType().toString());

        switch (cabinet.getType()) {
            case ADSL:
                header.setBackgroundColor(getResources().getColor(R.color.adsl_red));
                break;
            case VDSL:
                header.setBackgroundColor(getResources().getColor(R.color.vdsl_green));
                break;
        }

        cabinetCoordinates.setText(cabinet.getCoordinates().toString());

        if (cabinet.getImage() == null) {
            loadCabinetImage(marker);
        } else {
            cabinetImage.setImageBitmap(cabinet.getImage());
        }

        if (cabinet.getUserNick() == null) {
            loadCabinetUserNick(cabinet, cabinet.getUserId(), cabinet.getmUserSite(), marker);
        } else {
            cabinetUserNick.setText("Added by user: " + cabinet.getUserNick());
        }

        if (cabinet.getAddress() == null) {
            loadCabinetAddress(cabinet.getCoordinates().latitude,
                    cabinet.getCoordinates().longitude, marker);
        } else {
            cabinetAddress.setText("Address: " + cabinet.getAddress());
        }

        return view;
    }

    private final View getDslamView (final Cabinet cabinet, final Marker marker) {
        View view = getLayoutInflater().inflate(R.layout.dslam_info_window, null);

        TextView dslamId = (TextView) view.findViewById(R.id.dslam_id);
        TextView dslamAddress = (TextView) view.findViewById (R.id.dslam_address);
        TextView dslamCoordinates = (TextView) view.findViewById (R.id.dslam_coordinates);

        dslamId.setText(cabinet.getId() + " - " + cabinet.getCabinetNumber());
        dslamCoordinates.setText(cabinet.getCoordinates().toString());

        if (cabinet.getAddress() == null) {
            Log.i("fttxgr", "address is null");
            loadCabinetAddress(cabinet.getCoordinates().latitude,
                    cabinet.getCoordinates().longitude, marker);
        } else {
            Log.i("fttxgr", "address is there: " + cabinet.getAddress());
            dslamAddress.setText("Address: " + cabinet.getAddress());
        }

        return view;
    }

}

还有 loadCabinetAddress() 函数:

private void loadCabinetAddress (final double lat, final double lng, final Marker marker) {
    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());

    try {
        addresses = geocoder.getFromLocation(lat, lng, 1);
        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getLocality();
        String country = addresses.get(0).getCountryName();
        String postalCode = addresses.get(0).getPostalCode();

        String concat = "" +
                ((address != null) ? address : "") + " - " +
                ((city != null) ? city : "") + " - " +
                ((country != null) ? country : "") + " - " +
                ((postalCode != null) ? postalCode : "");

        clickedCabinetMarker.getCabinet().setAddress(concat);
        Log.i("fttxgr", "address loaded, showing info window again");
        marker.showInfoWindow();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

所有日志都表明一切都按预期进行。那么为什么信息窗口没有正确更新呢?

附:有趣的是,对于第一个视图(由 getCabinetView() 返回的那个),更新是有效的!但是我也异步加载了一张图片和一个用户昵称,我调用了 3 次 showInfoWindow() 来更新它们。

【问题讨论】:

    标签: android google-maps infowindow


    【解决方案1】:

    如果你想为集群中的每个项目放置一个信息窗口,你可以直接使用 setOnClusterItemClickListener 方法。整个集群,您可以使用 setOnClusterClickListener 方法创建它,而不是做所有这些繁重的工作。

    创建一个 ClusterManager 并使用适配器设置信息窗口:

    ClusterManager<MarkerItem> clusterMgr = new ClusterManager<MarkerItem>(context, map);
    map.setInfoWindowAdapter(clusterMgr.getMarkerManager());
    

    为其中之一创建infowindowadapter

    clusterMgr.getMarkerCollection().setOnInfoWindowAdapter(new MyCustomAdapterForItems());
    

    最后一部分是将您在自定义 InfoWindowAdapter 的回调中收到的原始 Marker 对象映射到您首先添加到地图中的 ClusterItem 对象。这可以使用 onClusterClick 和 onClusterItemClick 侦听器来实现,如下所示:

    map.setOnMarkerClickListener(clusterMgr);
    clusterMgr.setOnClusterItemClickListener(new OnClusterItemClickListener<MarkerItem>() {
        @Override
        public boolean onClusterItemClick(MarkerItem item) {
            clickedClusterItem = item;
            return false;
        }
    });
    

    【讨论】:

    • 您好,感谢您的回答。我已经完成了所有这些链接,信息窗口出现了,只是当我再次调用 showInfoWindow() 时它们没有得到更新。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多