`setInfoWindowAdapter`无法渲染标记物信息窗口。

huangapple go评论79阅读模式
英文:

setInfoWindowAdapter does not render marker info windows

问题

这是您提供的代码部分的翻译:

这是我的第一个问题对于格式或其他任何问题很抱歉

我正在尝试向由Google工具的ClusterManager聚类的标记添加信息窗口但我的自定义InfoWindowAdapter不会呈现我的项目这就是我正在尝试做的事情

在onMapReady回调之后我像这样初始化聚类管理器
```java
        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, "从标记获取信息内容");
        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", "点击查看更多信息"));

                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;
    }
}

当调用此类时,getInfoContents永远不会执行,也不会执行getInfoWindow,尽管我尝试记录消息,但仍然没有来自此类的输出,只有来自构造函数。您能帮助我吗?


请注意,代码中有一些HTML实体编码(例如`&quot;`),您需要将它们替换为正常的引号字符(`"`)以使代码正常运行。如果您有进一步的问题或需要帮助,可以随时提出。
<details>
<summary>英文:</summary>
it&#39;s my first question, so sorry for bad formatting or anything like this.
I&#39;m trying to add info windows to markers, which are clustered by ClusterManager from google utils, but my item does not get rendered by my custom InfoWindowAdapter and that&#39;s what i&#39;m trying to do. 
After onMapReady callback, i initialize cluster manager like this:
    mClusterManager = new ClusterManager&lt;&gt;(this, mMap);
mClusterManager.setRenderer(new ClusterMarkerRenderer(this, mMap, mClusterManager));
mClusterManager.setOnClusterClickListener(cluster -&gt; {
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 Class (only important parts):

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() + &quot;#&quot; + item.getPosition().latitude
+ &quot;#&quot; + item.getPosition().longitude + &quot;#&quot; + item.getRotation() + &quot;#&quot; + item.getIconName() + &quot;#&quot; + item.getName());
super.onBeforeClusterItemRendered(item, markerOptions);
}
@Override
protected void onClusterItemRendered(@NotNull CarMapItem item, @NotNull Marker marker) {
super.onClusterItemRendered(item, marker);
marker.setTag(item);
}

}


This is where i set marker options, before cluster item gets rendered, my icon is set and everything is fine, but after rendering, i just set my item as tag to retrieve information for drawing and it won&#39;t render anything after this. And there is my last class, InfoWindowCarAdapter:

public class InfoWindowCarAdapter implements GoogleMap.InfoWindowAdapter {

private View myContentsView;
private Activity context;
@SuppressLint(&quot;InflateParams&quot;)
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, &quot;GET INFO CONTENTS FROM MARKER&quot;);
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(), &quot;%s&quot;, 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(), &quot;%s&quot;, &quot;Click for more info&quot;));
if (object.getCarEngineStatus() == 1) {
title.setText(String.format(Locale.getDefault(), &quot;%d KM/h&quot;, 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;
}

}


When this class gets called, get info contents never does neither get info window, even tho i tried to log messages, still no output from this class, only from constructor. 
Can u help me?
</details>
# 答案1
**得分**: 0
如果有人需要的话,我已经自行修复了,错误在于:在我的活动中,当我将InfoWindowAdapter设置为Google地图对象时,我必须将ClusterManager的MarkerManager设置为Google地图对象,然后将我的自定义信息窗口适配器设置为cluster manager的标记集合,就像这样:
```java
mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
mClusterManager.getMarkerCollection().setInfoWindowAdapter(new InfoWindowCarAdapter(this));

如果有人需要,就在这里 `setInfoWindowAdapter`无法渲染标记物信息窗口。

英文:

If anyone needs, i fixed it myself, and here is the mistake: In my activity, where i set InfoWindowAdapter to google maps object, i had to set ClusterManager's MarkerManager to Google Maps object and then set my custom info window adapter to cluster manager's marker collection just like this:

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

if anybody needs this, it's here `setInfoWindowAdapter`无法渲染标记物信息窗口。

huangapple
  • 本文由 发表于 2020年7月28日 23:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63137394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定