本文介绍了地图集群 - 最大缩放标记仍然聚集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android maps utils来聚合google maps api v2上的标记。它运行良好,但是当我添加了2000多个标记时,在最大缩放上它仍然是聚集的(标记仍然有数字): i.stack.imgur.com/pDd2Q.pngalt =2000+ marker>

这是我用标记填充地图的方法:

  public void getRiverData(String state,String type){
URL = getResources()。getString(R.string.base_url)+ state +/+ type
+ getResources()。getString(R .string.end_url);
SimpleXmlRequest< XMLData> simpleRequest = new SimpleXmlRequest< XMLData>(
URL,XMLData.class,new Response.Listener< XMLData>(){
$ b @Override
public void onResponse(XMLData response){

//使用上下文和映射初始化管理器
//(Activity扩展上下文,所以我们可以在
中传递'this'//构造函数。)
mClusterManager = new ClusterManager< MarkerItem>(
getActivity(),map);

mClusterManager.setRenderer(new ClusterRenderer(
getActivity(),map,mClusterManager)) ;

//响应对象
waterItemsList = response.getNews();
$ b $ for(News item:waterItemsList){
if(item.getRiver_name ()!= null
&安培;&安培; item.getRiver_name()!=){

water_level_value = item.getWater_level_value();

if(water_level_value!= null
&&!water_level_value.equals(0)
&&!water_level_value.equals()){

MarkerItem offsetItem = new MarkerItem(item);
mClusterManager.addItem(offsetItem);

}

map.setOnMarkerClickListener(mClusterManager);

map.setInfoWindowAdapter(new InfoWindowAdapter(){

@Override
public View getInfoWindow(Marker marker){

return null;
}

@Override
public View getInfoContents(Marker marker){

try {
View v = getActivity()
.getLayoutInflater()
.inflate(
R.layout.marker_info,
null);
TextView title =(TextView)v
.findV iewById(R.id.tvMarkerTitle);
TextView info =(TextView)v
.findViewById(R.id.tvMarkerInfo);

title.setText(marker.getTitle()
.toString());
info.setText(marker.getSnippet()
.toString());
return v;
} catch(Exception e){
//kliknięciew cluster
return null;

}

}
});

}

}

map.setOnCameraChangeListener(mClusterManager);

map.setOnInfoWindowClickListener(mClusterManager);

mClusterManager.cluster();


$ b},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
//错误对象
error.printStackTrace();
}
});

AppController.getInstance()。addToRequestQueue(simpleRequest);
}

任何人都可以帮我吗?为什么它不起作用?

解决方案

您可以扩展DefaultClusterRenderer类并将最小标记设置为集群。 b

  public class InfoMarkerRenderer扩展了DefaultClusterRenderer< MyCustomMarker> {

public InfoMarkerRenderer(Context context,GoogleMap map,ClusterManager< MyCustomMarker> clusterManager){
super(context,map,clusterManager);
//构造函数
}

@Override
保护无效onBeforeClusterItemRendered(最终MyCustomMarker infomarker,MarkerOptions markerOptions){
//您可以更改标记选项
}

@Override
protected boolean shouldRenderAsCluster(Cluster cluster){
return cluster.getSize()> 5; //如果标记< = 5,则不聚类
}
}


I'm using android maps utils for clustering the markers on google maps api v2. It worked fine, but when I added 2000+ markers, on max zoom it is still clustered (markers still have numbers):

Here is my method for filling map with markers:

public void getRiverData(String state, String type) {
    URL = getResources().getString(R.string.base_url) + state + "/" + type
            + getResources().getString(R.string.end_url);
    SimpleXmlRequest<XMLData> simpleRequest = new SimpleXmlRequest<XMLData>(
            URL, XMLData.class, new Response.Listener<XMLData>() {

                @Override
                public void onResponse(XMLData response) {

                    // Initialize the manager with the context and the map.
                    // (Activity extends context, so we can pass 'this' in
                    // the constructor.)
                    mClusterManager = new ClusterManager<MarkerItem>(
                            getActivity(), map);

                    mClusterManager.setRenderer(new ClusterRenderer(
                            getActivity(), map, mClusterManager));

                    // response Object
                    waterItemsList = response.getNews();

                    for (News item : waterItemsList) {
                        if (item.getRiver_name() != null
                                && item.getRiver_name() != "") {

                            water_level_value = item.getWater_level_value();

                            if (water_level_value != null
                                    && !water_level_value.equals(0)
                                    && !water_level_value.equals("")) {

                                MarkerItem offsetItem = new MarkerItem(item);
                                mClusterManager.addItem(offsetItem);

                            }

                            map.setOnMarkerClickListener(mClusterManager);

                            map.setInfoWindowAdapter(new InfoWindowAdapter() {

                                @Override
                                public View getInfoWindow(Marker marker) {

                                    return null;
                                }

                                @Override
                                public View getInfoContents(Marker marker) {

                                    try {
                                        View v = getActivity()
                                                .getLayoutInflater()
                                                .inflate(
                                                        R.layout.marker_info,
                                                        null);
                                        TextView title = (TextView) v
                                                .findViewById(R.id.tvMarkerTitle);
                                        TextView info = (TextView) v
                                                .findViewById(R.id.tvMarkerInfo);

                                        title.setText(marker.getTitle()
                                                .toString());
                                        info.setText(marker.getSnippet()
                                                .toString());
                                        return v;
                                    } catch (Exception e) {
                                        // kliknięcie w cluster
                                        return null;

                                    }

                                }
                            });

                        }

                    }

                    map.setOnCameraChangeListener(mClusterManager);

                    map.setOnInfoWindowClickListener(mClusterManager);

                    mClusterManager.cluster();

                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error Object
                    error.printStackTrace();
                }
            });

    AppController.getInstance().addToRequestQueue(simpleRequest);
}

Can anyone help me? Why is it not working?

解决方案

You can extend DefaultClusterRenderer class and set minimum markers to cluster.

public class InfoMarkerRenderer extends DefaultClusterRenderer<MyCustomMarker> {

public InfoMarkerRenderer(Context context, GoogleMap map, ClusterManager<MyCustomMarker> clusterManager) {
    super(context, map, clusterManager);
    //constructor
}

@Override
protected void onBeforeClusterItemRendered(final MyCustomMarker infomarker, MarkerOptions markerOptions) {
      // you can change marker options
}

@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
    return cluster.getSize() > 5; // if markers <=5 then not clustering
}
}

这篇关于地图集群 - 最大缩放标记仍然聚集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:57