4
我實現了一個選項以從實際位置查找最近的標記。我使用一個自定義InfoWindow來獲取ClusterItem中的所有標記。在沒有集羣的正常谷歌地圖上,我可以使用marker.showInfoWindow();
,InfoWindow會彈出。看起來在使用聚類時沒有這種方法,因爲標記不會被添加爲適當的地圖標記。Android在羣集標記上打開InfoWindow
我的代碼:
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;
}
}
}
你已經看過http://stackoverflow.com/questions/21885225/showing-custom-infowindow-for-android-maps-utility-library-for-android? –