英文:
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实体编码(例如`"`),您需要将它们替换为正常的引号字符(`"`)以使代码正常运行。如果您有进一步的问题或需要帮助,可以随时提出。
<details>
<summary>英文:</summary>
it's my first question, so sorry for bad formatting or anything like this.
I'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's what i'm trying to do.
After onMapReady callback, i initialize cluster manager like this:
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 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() + "#" + 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);
}
}
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'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("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;
}
}
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));
如果有人需要,就在这里
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论