1

是否有任何通用方法可以在Android 应用程序中为 Google 地图实现标记集群?

我有一个包含大约 10,000 个位置的数据库,并且希望以更智能的方式显示它们,而不仅仅是将数千个标记倾倒在一个很少比匹配框大的显示器上......理想情况下,应该可以从网站检索标记URL,尽管从可用性的角度来看,我不确定为此使用通常的 Async 函数是否有意义。

4

3 回答 3

3

Clusterkraf会为你工作。示例应用程序中高级模式的选项之一是在全球范围内聚集 10,000 个随机点。集群是在后台线程上完成的,因此即使在较旧的设备上也能正常工作。让它在大型数据集上执行的关键是将“扩展边界因子(过度绘制)”设置为 0。它在我的 Galaxy Nexus 上运行。

于 2013-04-29T20:50:17.803 回答
2

下面的代码对我有用:

主班

                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;
    }
}
于 2015-11-03T07:40:22.203 回答
0

我使用了这个解决方案,它对我来说效果很好,我希望它有所帮助。

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

于 2013-02-25T07:50:33.153 回答