第二次调用showInfoWindow()不会更新信息窗口

时间:2015-07-25 15:13:30

标签: android google-maps infowindow

这是事情: 我有一个带有标记的地图(由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();
    }
}

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

P.S。有趣的是,对于第一个视图(由getCabinetView()返回的视图),更新正在运行!但我也异步加载一个图像和一个用户昵称,我调用showInfoWindow()三次来更新它们。

1 个答案:

答案 0 :(得分:0)

如果要为群集中的每个项目设置信息窗口,如果要放置单个信息窗口,则可以直接使用 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;
    }
});