我试着在androidMap上创建一个带有集群的自定义标记。所以我创造了 MarkerCluster
clas和 ClusterManagerRender
它确实在Map上呈现,问题是,它并不总是这样。我不确定是否有默认值,因为现在,除了用户位置的当前标记外,Map上只有两个位置,而且只显示一个!
public class MarkerCluster implements ClusterItem {
private LatLng position;
private String title;
private String snippet;
private String image;
private Model model;
public MarkerCluster(LatLng position, String title, String snippet, String image, Model model) {
this.position = position;
this.title = title;
this.snippet = snippet;
this.image = image;
this.model = model;
}
public MarkerCluster(){}
@Override
public LatLng getPosition() {
return position;
}
public void setPosition(LatLng position) {
this.position = position;
}
@Override
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String getSnippet() {
return snippet;
}
public void setSnippet(String snippet) {
this.snippet = snippet;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
经理:
public class ClusterManagerRender extends DefaultClusterRenderer<MarkerCluster> {
private final IconGenerator iconGenerator;
private final ImageView imageView;
private final int markerWidth;
private final int markerHeight;
public ClusterManagerRender(Context context, GoogleMap map, ClusterManager<MarkerCluster> clusterManager) {
super(context, map, clusterManager);
iconGenerator = new IconGenerator(context.getApplicationContext());
imageView = new ImageView(context.getApplicationContext());
markerWidth = (int) context.getResources().getDimension(R.dimen.custom_marker_image);
markerHeight = (int) context.getResources().getDimension(R.dimen.custom_marker_image);
imageView.setLayoutParams(new ViewGroup.LayoutParams(markerWidth,markerHeight));
int padding = (int) context.getResources().getDimension(R.dimen.custom_marker_padding);
imageView.setPadding(padding,padding,padding,padding);
iconGenerator.setContentView(imageView);
}
@Override
protected void onBeforeClusterItemRendered(MarkerCluster item, MarkerOptions markerOptions) {
Picasso.get().load(item.getImage()).into(imageView);
Bitmap bitmap = iconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap)).title(item.getTitle());
}
@Override
protected boolean shouldRenderAsCluster(Cluster<MarkerCluster> cluster) {
return false;
}
以及服务器的响应:
private void DrawPinsOnMap(List<com.corp.barros.brasil.initiallocation.Model> list){
if(clusterManager == null){
clusterManager = new ClusterManager<MarkerCluster>(getApplicationContext(),mMap);
}
if(clusterManagerRender == null){
clusterManagerRender = new ClusterManagerRender(getApplicationContext(),mMap,clusterManager);
clusterManager.setRenderer(clusterManagerRender);
}
clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MarkerCluster>() {
@Override
public boolean onClusterItemClick(MarkerCluster markerCluster) {
com.corp.barros.brasil.initiallocation.Model mm = markerCluster.getModel();
return false;
}
});
for (com.corp.barros.brasil.initiallocation.Model m: list) {
String ti = null;
String sn = null;
if(m.getName() != null){
ti = m.getName();
}else {
ti = "Anônimo";
}
MarkerCluster markerCluster = new MarkerCluster(new LatLng(m.getLat(),m.getLong()),ti,m.getTitle(),m.getUrl(),m);
clusterManager.addItem(markerCluster);
markerClusters.add(markerCluster);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!