gpt4 book ai didi

android - GoogleMap android 中的 ClusterManager

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:50 25 4
gpt4 key购买 nike

我想在谷歌地图上实现标记聚类。它也应该涉及自定义渲染。所以我使用了 https://github.com/googlemaps/android-maps-utils 中的示例代码,

引用演示中的 CustomMarkerClusteringDemoActivity.java。

我的问题是,当我单击集群时,它不会调用 onClusterClick()。与单击集群项类似,不会调用 onClusterItemClick()。

我不明白哪里出错了,请帮忙....

我正在使用:

第 1 步:

private ClusterManager<MarkerCluster> mClusterManager;

第 2 步:

private class MarkerRenderer extends DefaultClusterRenderer<MarkerCluster> {

private final IconGenerator mIconGenerator =
new IconGenerator(context.getApplicationContext());
private final IconGenerator mClusterIconGenerator =
new IconGenerator(context.getApplicationContext());
private final ImageView mImageView = null;
private final TextView mClusterTextCounter;
private final ImageView mClusterImageView;
private final int mDimension = 0;

public MarkerRenderer() {
super(context.getApplicationContext(), mMap, mClusterManager);

View multiProfile = getActivity().getLayoutInflater()
.inflate(R.layout.layout_marker_render, null);
mClusterIconGenerator.setContentView(multiProfile);
mClusterImageView = (ImageView)multiProfile
.findViewById(R.id.lyt_marker_render_img_pin);
mClusterTextCounter = (TextView) multiProfile
.findViewById(R.id.lyt_marker_render_txt_counter);

}

@Override
protected void onBeforeClusterItemRendered(MarkerCluster markercluster, MarkerOptions markerOptions) {
// Draw a single person.
// Set the info window to show their name.
Item mItem = markercluster.getItem();

if(mItem != null){

if(mItem.get_UserType().equalsIgnoreCase("2")){
markerOptions
.title(mItem.get_maker() + " - " + mItem.get_carModel())
.icon(icon_blue);
}else{
markerOptions
.title(mItem.get_maker() + " - " + mItem.get_carModel())
.icon(icon_gray);
}

}
}

@Override
protected void onBeforeClusterRendered(Cluster<MarkerCluster> cluster, MarkerOptions markerOptions) {
// Draw multiple people.
// Note: this method runs on the UI thread. Don't spend too much time in here (like in this example).
mClusterTextCounter.setText(cluster.getItems().size() + "");

Drawable myDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
myDrawable = context.getResources()
.getDrawable(R.drawable.pin_blue, context.getTheme());
} else {
myDrawable = context.getResources()
.getDrawable(R.drawable.pin_blue);
}

mClusterImageView.setImageDrawable(myDrawable);

mClusterIconGenerator.setBackground(
context.getResources().getDrawable(R.drawable.map_btn)
);

Bitmap icon = mClusterIconGenerator.makeIcon(
String.valueOf(cluster.getSize())
);

markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}

@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
}

第 3 步:

mClusterManager = new ClusterManager<MarkerCluster>(context, mMap);
mClusterManager.setRenderer(new MarkerRenderer());

mMap.setOnMarkerClickListener(mClusterManager);
mMap.setOnInfoWindowClickListener(mClusterManager);
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {

if (mClusterManager != null) {
mClusterManager.onCameraChange(cameraPosition);
}

// Some Other Tasks here

}
});

mClusterManager.setOnClusterClickListener(
new ClusterManager.OnClusterClickListener<MarkerCluster>() {
@Override
public boolean onClusterClick(Cluster<MarkerCluster> cluster) {
LatLngBounds.Builder builder = LatLngBounds.builder();
for (MarkerCluster item : cluster.getItems()) {
builder.include(item.getPosition());
}
final LatLngBounds bounds = builder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));

return true;
}
});


mClusterManager.setOnClusterItemClickListener(
new ClusterManager.OnClusterItemClickListener<MarkerCluster>() {
@Override
public boolean onClusterItemClick(MarkerCluster markerCluster) {

Item item = markerCluster.getItem();
if (mItem != null) {

MarkerClickDialog mMarkerClickDialog = new MarkerClickDialog(context, mItem);
mMarkerClickDialog.show();
}

return false;
}
});

for (int i = 0; i < mItemList.size(); i++) {

Item mItem = mItemList.get(i);
if (mMap != null) {
displayLocation(
Double.parseDouble(mItem.get_Latitude()),
Double.parseDouble(mItem.get_Longitude()),
mItem
);
}
}

private void displayLocation(double latitude, double longitude, Item mItem){

// create marker
if(mMap != null){
Marker mMarker = null;

MarkerCluster offsetItem = new MarkerCluster(new LatLng(latitude, longitude), "Test", R.drawable.pin_blue, mItem);
mClusterManager.addItem(offsetItem);
mClusterManager.cluster();
}
}

最佳答案

在您的 MarkerRenderer 构造函数中,有 super(context.getApplicationContext(), mMap, mClusterManager);

您仍然需要 GoogleMap 对象和 ClusterManager。您应该将参数传递给构造函数!像这样写:

public CustomRenderer(Context context, GoogleMap map, ClusterManager clusterManager) {
super(context, map, clusterManager);
...

然后在设置Renderer的时候,这样写

mClusterManager.setRenderer(new MarkerRenderer(mContext, mMap, mClusterManager));

关于android - GoogleMap android 中的 ClusterManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com