我想在Android中显示一个包含所有标记图像的信息窗口。我提到了Google Map Cluster Tutorial。
答案 0 :(得分:27)
MyItem
上课:
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterItem;
public class MyItem implements ClusterItem {
private LatLng mPosition;
private String mLatitude = "";
private String mStoreLogo = "";
private String mLongitude = "";
@Override
public LatLng getPosition() {
return mPosition;
}
public void setPosition(LatLng mPosition) {
this.mPosition = mPosition;
}
public LatLng getmPosition() {
return mPosition;
}
public void setmPosition(LatLng mPosition) {
this.mPosition = mPosition;
}
public String getmLatitude() {
return mLatitude;
}
public void setmLatitude(String mLatitude) {
this.mLatitude = mLatitude;
}
public String getmLongitude() {
return mLongitude;
}
public void setmLongitude(String mLongitude) {
this.mLongitude = mLongitude;
}
public String getmStoreLogo() {
return mStoreLogo;
}
public void setmStoreLogo(String mStoreLogo) {
this.mStoreLogo = mStoreLogo;
}
}
Map
活动类:
public class Map extends FragmentActivity implements
ClusterManager.OnClusterClickListener<MyItem>,
ClusterManager.OnClusterInfoWindowClickListener<MyItem>,
ClusterManager.OnClusterItemClickListener<MyItem>,
ClusterManager.OnClusterItemInfoWindowClickListener<MyItem> {
private ClusterManager<MyItem> mClusterManager;
private Cluster<MyItem> clickedCluster;
private MyItem clickedClusterItem;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.map_view);
try {
// Initializing Map from XML :
GooglePlayServicesUtil.isGooglePlayServicesAvailable(Map.this);
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mMapView);
mMapView = mapFrag.getMap();
mMapView.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMapView.getUiSettings().setZoomControlsEnabled(true);
mMapView.getUiSettings().setCompassEnabled(true);
mMapView.getUiSettings().setMyLocationButtonEnabled(true);
mMapView.getUiSettings().setRotateGesturesEnabled(true);
mMapView.getUiSettings().setScrollGesturesEnabled(true);
mMapView.getUiSettings().setTiltGesturesEnabled(true);
mMapView.getUiSettings().setZoomGesturesEnabled(true);
mMapView.setMyLocationEnabled(true);
} catch (Exception e) {
mMapFrame.setVisibility(View.GONE);
Utils.displayToast("Your device doesn't support Google Map", Map.this);
}
// Creating cluster manager object.
mClusterManager = new ClusterManager<MyItem>(Map.this, mMapView);
mMapView.setOnCameraChangeListener(mClusterManager);
mClusterManager.setRenderer(new MyClusterRenderer(Map.this, mMapView,
mClusterManager));
mMapView.setOnInfoWindowClickListener(mClusterManager);
mMapView.setInfoWindowAdapter(mClusterManager.getMarkerManager());
mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(
new MyCustomAdapterForClusters());
mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(
new MyCustomAdapterForItems());
mMapView.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.setOnClusterInfoWindowClickListener(this);
mClusterManager.setOnClusterItemClickListener(this);
mClusterManager.setOnClusterItemInfoWindowClickListener(this);
mClusterManager
.setOnClusterClickListener(new OnClusterClickListener<MyItem>() {
@Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
clickedCluster = cluster;
return false;
}
});
mClusterManager
.setOnClusterItemClickListener(new OnClusterItemClickListener<MyItem>() {
@Override
public boolean onClusterItemClick(MyItem item) {
clickedClusterItem = item;
return false;
}
});
// Adding Objects to the Cluster.
mClusterManager.addItem(mItemData);
mMapView.animateCamera(CameraUpdateFactory
.newLatLngZoom(mLatLng, 7));
mClusterManager.cluster();
}
class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {
public MyClusterRenderer(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
@Override
protected void onBeforeClusterItemRendered(MyItem item,
MarkerOptions markerOptions) {
super.onBeforeClusterItemRendered(item, markerOptions);
}
@Override
protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
}
}
// Custom adapter info view :
public class MyCustomAdapterForItems implements InfoWindowAdapter {
private final View myContentsView;
MyCustomAdapterForItems() {
myContentsView = getLayoutInflater().inflate(
R.layout.map_info_window_dialog, null);
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(Marker marker) {
TextView tvTitle = ((TextView) myContentsView
.findViewById(R.id.txtHeader));
TextView tvSnippet = ((TextView) myContentsView
.findViewById(R.id.txtAddress));
tvTitle.setTypeface(mTyFaceKreonBold);
tvSnippet.setTypeface(mTyFaceKreonBold);
if (clickedClusterItem != null) {
tvTitle.setText(clickedClusterItem.getmStoreName());
tvSnippet.setText(clickedClusterItem.getmAddressOne());
}
return myContentsView;
}
}
// class for Main Clusters.
public class MyCustomAdapterForClusters implements InfoWindowAdapter {
private final View myContentsView;
MyCustomAdapterForClusters() {
myContentsView = getLayoutInflater().inflate(
R.layout.map_info_window_dialog, null);
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(Marker marker) {
TextView tvTitle = ((TextView) myContentsView
.findViewById(R.id.txtHeader));
TextView tvSnippet = ((TextView) myContentsView
.findViewById(R.id.txtAddress));
tvSnippet.setVisibility(View.GONE);
tvTitle.setTypeface(mTyFaceKreonBold);
tvSnippet.setTypeface(mTyFaceKreonBold);
if (clickedCluster != null) {
tvTitle.setText(String
.valueOf(clickedCluster.getItems().size())
+ " more offers available");
}
return myContentsView;
}
}
@Override
public void onClusterItemInfoWindowClick(MyItem item) {
Intent intent = new Intent(Map.this,NextActivity.class);
intent.putExtra("mLatitude", item.getmLatitude());
intent.putExtra("mLongitude", item.getmLongitude());
startActivity(intent);
finish();
}
@Override
public boolean onClusterItemClick(MyItem item) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onClusterInfoWindowClick(Cluster<MyItem> cluster) {
// TODO Auto-generated method stub
}
@Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
// TODO Auto-generated method stub
return false;
}
}
代码是自我解释的。请将群集库和google play服务lib添加到您的构建路径。如果您有任何疑问,请与我们联系。