【问题标题】:setInfoWindowAdapter does not render marker info windowssetInfoWindowAdapter 不呈现标记信息窗口
【发布时间】:2020-07-28 15:12:03
【问题描述】:

这是我的第一个问题,很抱歉格式错误或类似的问题。

我正在尝试将信息窗口添加到标记,这些标记由 Google utils 中的 ClusterManager 聚集,但我的项目没有由我的自定义 InfoWindowAdapter 呈现,这就是我想要做的。

在 onMapReady 回调之后,我像这样初始化集群管理器:

        mClusterManager = new ClusterManager<>(this, mMap);
        mClusterManager.setRenderer(new ClusterMarkerRenderer(this, mMap, mClusterManager));
        mClusterManager.setOnClusterClickListener(cluster -> {
            LatLngBounds.Builder builder = LatLngBounds.builder();
            for (CarMapItem item : cluster.getItems()) {
                builder.include(item.getPosition());
            }
            final LatLngBounds bounds = builder.build();
            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
            return true;
        });

        mMap.setInfoWindowAdapter(new InfoWindowCarAdapter(this));

ClusterMarkerRenderer 类(仅重要部分):

public class ClusterMarkerRenderer extends DefaultClusterRenderer<CarMapItem> {
    public ClusterMarkerRenderer(Context context, GoogleMap map, ClusterManager<CarMapItem> clusterManager) {
        super(context, map, clusterManager);
    }

    @Override
    protected void onBeforeClusterItemRendered(CarMapItem item, @NotNull MarkerOptions markerOptions) {
        if (item.getIcon() != null) {
            markerOptions.icon(item.getIcon());
        }

        if (!item.hasCustomBitmap()) {
            markerOptions.rotation(item.getRotation());
        }

        markerOptions.title(item.getName());
        markerOptions.anchor(0.5f, 0.5f);
        markerOptions.flat(true);
        markerOptions.infoWindowAnchor(0.5f, 0.5f);
        markerOptions.snippet(item.getImei() + "#" + item.getPosition().latitude
                + "#" + item.getPosition().longitude + "#" + item.getRotation() + "#" + item.getIconName() + "#" + item.getName());

        super.onBeforeClusterItemRendered(item, markerOptions);
    }
     
    @Override
    protected void onClusterItemRendered(@NotNull CarMapItem item, @NotNull Marker marker) {
        super.onClusterItemRendered(item, marker);
        marker.setTag(item);
    }
}

这是我设置标记选项的地方,在集群项目被渲染之前,我的图标被设置并且一切都很好,但是在渲染之后,我只是将我的项目设置为标签来检索绘图信息,之后它不会渲染任何东西这。还有我的最后一堂课,InfoWindowCarAdapter:

public class InfoWindowCarAdapter implements GoogleMap.InfoWindowAdapter {

    private View myContentsView;
    private Activity context;


    @SuppressLint("InflateParams")
    public InfoWindowCarAdapter(Activity context) {
        this.context = context;
        myContentsView = context.getLayoutInflater().inflate(R.layout.item_car_info, null);
    }

    @Override
    public View getInfoContents(Marker marker) {
        Log.e(TAG, "GET INFO CONTENTS FROM MARKER");
        CarMapItem object = (CarMapItem) marker.getTag();
        if (object != null) {
            TextView tvTitle = myContentsView.findViewById(R.id.txt_car_name);
            if (object.getItemType() == 1) {
                ((TextView) myContentsView.findViewById(R.id.txt_adit_info)).setText(String.format(Locale.getDefault(), "%s", Helper.getFormatedAlarmWithVars(context, object.getSpeed(),
                        object.getIconName(), object.getVar2())));
                (myContentsView.findViewById(R.id.txt_status)).setVisibility(View.GONE);
                (myContentsView.findViewById(R.id.ic_status_icon)).setVisibility(View.GONE);
            } else {
                TextView title = myContentsView.findViewById(R.id.txt_status);
                ImageView icon = myContentsView.findViewById(R.id.ic_status_icon);


                ((TextView) myContentsView.findViewById(R.id.txt_adit_info)).setText(String.format(Locale.getDefault(), "%s", "Click for more info"));

                if (object.getCarEngineStatus() == 1) {
                    title.setText(String.format(Locale.getDefault(), "%d KM/h", object.getSpeed()));
                } else {
                    title.setText(Helper.formatSecondPrecise(context, object.getLastActivityTime()));
                }

                icon.setImageResource(Helper.getIconResIdByCarStatus(object.getVehicleStatus()));

                (myContentsView.findViewById(R.id.txt_status)).setVisibility(View.VISIBLE);
                (myContentsView.findViewById(R.id.ic_status_icon)).setVisibility(View.VISIBLE);
            }


            tvTitle.setText(object.getName());

            return myContentsView;
        }
        return myContentsView;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

}

当此类被调用时,获取信息内容永远不会获取信息窗口,即使我尝试记录消息,仍然没有此类的输出,仅来自构造函数。

你能帮帮我吗?

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    如果有人需要,我自己修复了它,这是错误:在我的活动中,我将 InfoWindowAdapter 设置为谷歌地图对象,我必须将 ClusterManager 的 MarkerManager 设置为谷歌地图对象,然后将我的自定义信息窗口适配器设置为集群管理器的标记集合是这样的:

    mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
    mClusterManager.getMarkerCollection().setInfoWindowAdapter(new InfoWindowCarAdapter(this));
    

    如果有人需要这个,它就在这里 :)

    【讨论】:

    • setOnInfoWindowClickListener 也一样,你必须为 ClusterManager 的 getMarkerCollection 对象设置它。
    猜你喜欢
    • 2016-07-09
    • 1970-01-01
    • 2016-07-27
    • 2013-09-02
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多