Google Maps marker clusters in Android

2019-04-16 16:47发布

问题:

Is there any common way to implement marker clusters for Google Maps in Android apps?

I have a database of about 10,000 locations and would like to display them in more intelligent ways than just dumping thousands of markers on a display that's rarely larger than a match box... Ideally, it should be possible to retrieve the markers from a website URL, though I'm not sure if using the usual Async functions for that even makes sense from a usability point of view.

回答1:

Clusterkraf would work for you. One of the options in Advanced Mode in the sample app is to cluster 10,000 random points worldwide. The clustering is done on a background thread, so it works reasonably well even on older devices. The key to make it perform with large data sets is to set the "expand bounds factor (overdraw)" to 0. It flies on my Galaxy Nexus.



回答2:

Below Code is worked for me:

Main Class

                MapView mMapView;
                private GoogleMap googleMap;
                ClusterManager<MyItem> mClusterManager;


                        mClusterManager = new ClusterManager<MyItem>(getActivity(), googleMap);

                        mMapView = (MapView) v.findViewById(R.id.mapView);
                        mMapView.onCreate(savedInstanceState);

                        mMapView.onResume();// needed to get the map to display immediately

                        try {
                            MapsInitializer.initialize(getActivity().getApplicationContext());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                       googleMap = mMapView.getMap();

                       //below code use in onPost() of Asynctask
                       googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentlat,currentlng), 15));

                                        mClusterManager = new ClusterManager<MyItem>(getActivity(), googleMap);

                                        googleMap.setOnCameraChangeListener(mClusterManager);
                                        googleMap.setOnMarkerClickListener(mClusterManager);
                                        CustomRenderer customRenderer = new CustomRenderer(getActivity(), googleMap, mClusterManager);                          
                                         mClusterManager.setRenderer(customRenderer);


                                for(int i=0;i<jarray.length();i++)
                                {

                                    JSONObject jobj_root=jarray.getJSONObject(i);
                                    JSONObject j_j_record=jobj_root.getJSONObject("Record");
                                    lat=j_j_record.getDouble("latitude");
                                    lng=j_j_record.getDouble("longitude");

                                     Location loc1 = new Location("");
                                    loc1.setLatitude(temp_lat);
                                    loc1.setLongitude(temp_lng);

                                    Location loc2 = new Location("");
                                    loc2.setLatitude(lat);
                                    loc2.setLongitude(lng);

                                    float distanceInMeters = loc1.distanceTo(loc2);
                                    if(distanceInMeters<2000)
                                    {
                                        //Toast.makeText(getActivity(), " kresp="+distanceInMeters, 34).show();
                                         MyItem offsetItem = new MyItem(lat,lng);
                                            mClusterManager.addItem(offsetItem);
                                    }

                                }

CustomRenderer.class

public class CustomRenderer extends DefaultClusterRenderer<MyItem>{

    private boolean shouldCluster = true;
    private static final int MIN_CLUSTER_SIZE = 1;

    public CustomRenderer(Context context, GoogleMap map,ClusterManager<MyItem> clusterManager) {
        super(context, map, clusterManager);
        // TODO Auto-generated constructor stub
    }

    public void setMarkersToCluster(boolean toCluster)
    {
        this.shouldCluster = toCluster;
    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {

        if(shouldCluster)
        {
            return cluster.getSize() > MIN_CLUSTER_SIZE;
        }
        else
        {
            return shouldCluster;
        }

    }

}

MyItem.class

public class MyItem implements ClusterItem {
    private final LatLng mPosition;

    public MyItem(double lat, double lng) {
        mPosition = new LatLng(lat, lng);
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }
}


回答3:

I used this solution, and it worked pretty well for me, I hope it helps.

http://code.google.com/p/android-playground-erdao/wiki/PhotSpot