gpt4 book ai didi

Android 在集群标记上打开 InfoWindow

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:59 25 4
gpt4 key购买 nike

我实现了一个选项来找到离实际位置最近的标记。我在带有自定义信息窗口的 ClusterItem 中拥有所有标记。在没有聚类的普通谷歌地图上,我可以只使用 marker.showInfoWindow(); 并且 InfoWindow 会弹出。使用聚类时似乎没有这样的方法,因为标记没有被添加为正确的 map 标记。

我的代码:

public class StationsFragment extends Fragment implements OnMapReadyCallback {
private static GoogleMap googleMap;

private ClusterManager<MyItem> clusterManager;
private MyItem clickedClusterItem;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_activity, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

Utils.changeLanguage(getActivity());

final SupportMapFragment map = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);
}

@Override
public void onMapReady(final GoogleMap map) {
googleMap = map;

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(0.0, 0.0), 10));
map.setMyLocationEnabled(true);

clusterManager = new ClusterManager<>(getActivity(), map);

map.setOnCameraChangeListener(clusterManager);
map.setOnMarkerClickListener(clusterManager);
map.setInfoWindowAdapter(clusterManager.getMarkerManager());

clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
@Override
public boolean onClusterItemClick(MyItem item) {
clickedClusterItem = item;
return false;
}
});

loadMarkers();
}

private void loadMarkers() {
clusterManager.addItem(new MyItem(lat, lng, title, snippet));
}

public class ItemAdapter implements GoogleMap.InfoWindowAdapter {

private final View view;

ItemAdapter() {
view = getActivity().getLayoutInflater().inflate(R.layout.info_window, null);
}

@Override
public View getInfoWindow(Marker marker) {
TextView title = (TextView) view.findViewById(R.id.info_title);
title.setText(clickedClusterItem.getTitle());

TextView snippet = (TextView) view.findViewById(R.id.info_snippet);
snippet.setText(clickedClusterItem.getSnippet());

return view;
}

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

public class MyItem implements ClusterItem {
private final LatLng position;
private final String title;
private final String snippet;

public MyItem(double lat, double lng, String title, String snippet) {
this.position = new LatLng(lat, lng);
this.title = title;
this.snippet = snippet;
}

@Override
public LatLng getPosition() {
return position;
}

public String getTitle(){
return title;
}

public String getSnippet(){
return snippet;
}
}
}

最佳答案

看来你没有 ClusterManager 的 clusterManager.cluster() 方法一旦将标记项添加到 clusterManager,您需要添加调用此方法

集群定制请引用以下代码:

    private class CustomMapClusterRenderer<T extends ClusterItem> extends DefaultClusterRenderer<T> {
CustomMapClusterRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
super(context, map, clusterManager);
}

@Override
protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
//start clustering if 2 or more items overlap
return cluster.getSize() >= Constants.MINIMUM_CLUSTER_SIZE;
}

@Override
protected void onBeforeClusterItemRendered(T item,
MarkerOptions markerOptions) {
ClusterMarkerItem markerItem = (ClusterMarkerItem) item;
markerOptions.icon(markerItem.getmBitmapDescriptor());
}
@Override
protected void onBeforeClusterRendered(Cluster<T> cluster, MarkerOptions markerOptions) {
// for dynamically change custom map markericon
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.pin_cluster);
markerOptions.icon(descriptor);
}

@Override
protected void onClusterRendered(Cluster<T> cluster, Marker marker) {
super.onClusterRendered(cluster, marker);
//add infowindow to cluster icon
marker.setTitle("count");
marker.setSnippet("Total Count - " + cluster.getItems().size());
}
}

关于Android 在集群标记上打开 InfoWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963602/

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