我有这个活动:
public class MapViewer extends Activity {
private GoogleMap map;
private Database db = new Database(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewer);
try {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map != null) {
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setRotateGesturesEnabled(false);
this.addMerchantMarkers(new MarkerOptions());
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void addMerchantMarkers(MarkerOptions mo) {
SQLiteDatabase dbRead = db.getReadableDatabase();
Cursor result = dbRead.rawQuery("SELECT title, addr, lat, lon FROM users", null);
while(result.moveToNext()) {
map.addMarker(mo.position(new LatLng(result.getFloat(2), result.getFloat(3)))
.title(result.getString(0))
.snippet(result.getString(1))
);;
}
}
}
我通过以下方式更改了使用标记聚类:
public class MapViewer extends Activity {
private GoogleMap map;
private Database db = new Database(this);
private ClusterManager<MyItem> mClusterManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewer);
try {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map != null) {
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setRotateGesturesEnabled(false);
setUpClusterer();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
private void setUpClusterer() {
mClusterManager = new ClusterManager<MyItem>(this, map);
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
addItems();
}
private void addItems() {
SQLiteDatabase dbRead = db.getReadableDatabase();
Cursor result = dbRead.rawQuery("SELECT lat, lon, title, addr FROM users", null);
while(result.moveToNext()) {
MyItem offsetItem = new MyItem(result.getFloat(0), result.getFloat(1));
mClusterManager.addItem(offsetItem);
}
}
}
现在我不知道如何像以前的代码一样为每个标记添加标题、片段和图标。现在,如果我单击单个标记,则没有任何反应...如何得到它?
在这里,你应该使用ClusterManager本身。例如,在群集项目单击时设置:
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
@Override
public boolean onClusterItemClick(MyItem item) {
//put your code here
return false;
}
});
在集群管理器类中还有其他不同的方法。您需要一个不太明显的代码来将信息窗口分配给标记或集群 - 您应该使用以下代码:
mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new MarkerInfoWindowAdapter());
mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(new ClusterInfoWindow());
那里有 MarkerInfoWindowAdapter 和 ClusterWindowAdapter 是你的类,它实现了 GoogleMap.InfoWindowAdapter。
对于图标,这有点困难,因为聚类会将图标更改为默认值。您应该使用方法:
public void setRenderer(com.google.maps.android.clustering.view.ClusterRenderer<T> view)
您可以拥有自己的渲染器类并重写某些方法。例如,要设置自定义图标,请使用这样的类:
class OwnIconRendered extends DefaultClusterRenderer<MyItem> {
public OwnIconRendered(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
@Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.icon(item.getIcon());
markerOptions.snippet(item.getSnippet());
markerOptions.title(item.getTitle());
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
并以这样的方式使用它:
mClusterManager.setRenderer(new OwnIconRendered(activity.getApplicationContext(), getMap(), mClusterManager));