我正在尝试在Android上的Google地图上显示一个场地列表,这些场景可以在缩小时进行聚类,也可以在非聚集的情况下进行放大。
当UNCLUSTERED时,可以打开单个项目信息窗口以查看该场地详细信息,然后单击以打开单独的活动。
我正在使用此https://developers.google.com/maps/documentation/android-api/utility/marker-clustering?hl=en
我这样做:
在onResume()中获取地图片段
used_memory:8519088
used_memory_human:8.12M
MapReadyCallback:
@Override
public void onResume() {
super.onResume();
// Getting map for the map fragment
mapFragment = new SupportMapFragment();
mapFragment.getMapAsync(new VenuesInLocationOnMapReadyCallback(getContext()));
// Adding map fragment to the view using fragment transaction
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.venues_in_location_support_map_fragment_container, mapFragment);
fragmentTransaction.commit();
}
设置Cluster Manager
private class VenuesInLocationOnMapReadyCallback implements OnMapReadyCallback {
private static final float ZOOM_LEVEL = 10;
private final Context context;
public VenuesInLocationOnMapReadyCallback(Context context) {
this.context = context;
}
@Override
public void onMapReady(final GoogleMap map) {
// Setting up marker clusters
setUpClusterManager(getContext(), map);
// Allowing user to select My Location
map.setMyLocationEnabled(true);
// My location button handler to check the location setting enable
map.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
promptForLocationSetting(getContext(), map);
// Returning false ensures camera try to move to user location
return false;
}
});
map.getUiSettings().setMyLocationButtonEnabled(true);
// Disabling map toolbar
map.getUiSettings().setMapToolbarEnabled(false);
}
}
添加群集项(标记)
private void setUpClusterManager(final Context context, GoogleMap map) {
// Declare a variable for the cluster manager.
ClusterManager<LocationMarker> mClusterManager;
// Position the map.
LatLng wocLatLng = new LatLng(28.467948, 77.080685);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(wocLatLng, VenuesInLocationOnMapReadyCallback.ZOOM_LEVEL));
// Initialize the manager with the context and the map.
mClusterManager = new ClusterManager<LocationMarker>(context, map);
// Point the map's listeners at the listeners implemented by the cluster
// manager.
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
// Add cluster items (markers) to the cluster manager.
addLocations(mClusterManager);
// Setting custom cluster marker manager for info window adapter
map.setInfoWindowAdapter(mClusterManager.getMarkerManager());
mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new MyLocationInfoWindowAdapter());
map.setOnInfoWindowClickListener(new MyMarkerInfoWindowClickListener());
}
MyLocationInfoWIndowAdapter
private void addLocations(ClusterManager<LocationMarker> mClusterManager) {
for (int i = 0; i < venuesDetailsJsonArray.length(); i++) {
try {
JSONObject thisVenueJson = (JSONObject) venuesDetailsJsonArray.get(i);
JSONObject thisVenueLocationJson = thisVenueJson.getJSONObject("location");
LocationMarker thisVenueMarker = new LocationMarker(thisVenueLocationJson.getDouble("latitude"),
thisVenueLocationJson.getDouble("longitude"), thisVenueJson.getInt("id"));
mClusterManager.addItem(thisVenueMarker);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
MarkerInfoWindowClickListener
private class MyLocationInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
Log.e("getInfoContent", marker.toString());
View venueInfoWindow = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.venues_map_item, null);
return venueInfoWindow;
}
}
位置标记类
private class MyMarkerInfoWindowClickListener implements GoogleMap.OnInfoWindowClickListener {
@Override
public void onInfoWindowClick(Marker marker) {
// TODO: This is the click listener, that means all the info must be added as Tag to Marker
Intent venueDetailsDisplayIntent = new Intent(getActivity(), VenueDetailsDisplayActivity.class);
startActivity(venueDetailsDisplayIntent);
}
}
}
我理解流程的方式是:
onResume - &gt; fragmentTransaction - &gt; VenuesInLocationOnMapReadyCallback - &gt; setUpClusterManager - &gt; addLocations(这会添加自定义标记)
标记点击 - &gt; MyLocationInfoWindowAdapter - &gt; getInfoContents(标记标记)
标记信息窗口点击 - &gt; MyMarkerInfoWindowClickListener
根据我对过程的理解(我可能错了):
在addLocations函数中添加标记时,我在自定义LocationMarker中添加了一个id。
我需要在infoWindow中显示不同标记的不同信息。
使用MyLocationInfoWindowAdapter显示InfoWindow - &gt; getInfoContents(标记标记)
但是这就是问题,我无法找到一种方法来确定点击了哪个标记,以便我可以在InfoWindow中设置适当的信息。
单击打开的InfoWindow我需要打开一个单独的Activity。 A / C给我InfoWindow点击是使用MyMarkerInfoWindowClickListener处理 - &gt; onInfoWindowClick(标记标记)这里我也有同样的问题(我无法弄清楚哪个标记的信息窗口已被点击)。