如何在android maps中设置地图群集的半径

如何在android maps中设置地图群集的半径,android,android-maps-utils,Android,Android Maps Utils,我是一个android开发的初学者,我正在进行android地图聚类,但是我不能设置聚类半径,有人能帮我吗 示例: 我想在某个固定距离范围内对标记进行聚类,例如,如果我给定半径1km,则应将一km范围内的所有标记进行聚类 我指的是集群开发您可以使用下面的代码来实现地图集群 static int OFFSET = 268435456; static double RADIUS = 85445659.4471; static double p

我是一个android开发的初学者,我正在进行android地图聚类,但是我不能设置聚类半径,有人能帮我吗

示例: 我想在某个固定距离范围内对标记进行聚类,例如,如果我给定半径1km,则应将一km范围内的所有标记进行聚类


我指的是集群开发

您可以使用下面的代码来实现地图集群

     static int OFFSET = 268435456;
                static double RADIUS = 85445659.4471;
        static double pi = 3.1444;

    public static double lonToX(double lon) {
        return Math.round(OFFSET + RADIUS * lon * pi / 180);
    }

    public static double latToY(double lat) {
        return Math.round(OFFSET
                - RADIUS
                * Math.log((1 + Math.sin(lat * pi / 180))
                        / (1 - Math.sin(lat * pi / 180))) / 2);
    }

    public static int pixelDistance(double lat1, double lon1, double lat2,
            double lon2, int zoom) {
        double x1 = lonToX(lon1);
        double y1 = latToY(lat1);

        double x2 = lonToX(lon2);
        double y2 = latToY(lat2);

        return (int) (Math
                .sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2))) >> (21 - zoom);
    }

    static ArrayList<Cluster> cluster(ArrayList<Marker> markers, int zoom) {

        ArrayList<Cluster> clusterList = new ArrayList<Cluster>();

        ArrayList<Marker> originalListCopy = new ArrayList<Marker>();

        for (Marker marker : markers) {
            originalListCopy.add(marker);
        }

        /* Loop until all markers have been compared. */
        for (int i = 0; i < originalListCopy.size();) {

            /* Compare against all markers which are left. */

            ArrayList<Marker> markerList = new ArrayList<Marker>();
            for (int j = i + 1; j < markers.size();) {
                int pixelDistance = pixelDistance(markers.get(i).getLatitude(),
                        markers.get(i).getLongitude(), markers.get(j)
                                .getLatitude(), markers.get(j).getLongitude(),
                        zoom);

                if (pixelDistance < 40) {


                    markerList.add(markers.get(j));

                    markers.remove(j);

                    originalListCopy.remove(j);
                    j = i + 1;
                } else {
                    j++;
                }

            }

            if (markerList.size() > 0) {
 markerList.add(markers.get(i));                
Cluster cluster = new Cluster(clusterList.size(), markerList,
                        markerList.size() + 1, originalListCopy.get(i)
                                .getLatitude(), originalListCopy.get(i)
                                .getLongitude());
                clusterList.add(cluster);
                originalListCopy.remove(i);
                markers.remove(i);
                i = 0;

            } else {
                i++;
            }

            /* If a marker has been added to cluster, add also the one */
            /* we were comparing to and remove the original from array. */

        }
        return clusterList;
    }

Just pass in your array list here containing latitude and longitude then to display clusters. Here goes the function.


    @Override
    public void onTaskCompleted(ArrayList<FlatDetails> flatDetailsList) {

        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        originalListCopy = new ArrayList<FlatDetails>();
        ArrayList<Marker> markersList = new ArrayList<Marker>();
        for (FlatDetails detailList : flatDetailsList) {

            markersList.add(new Marker(detailList.getLatitude(), detailList
                    .getLongitude(), detailList.getApartmentTypeString()));

            originalListCopy.add(detailList);

            builder.include(new LatLng(detailList.getLatitude(), detailList
                    .getLongitude()));

        }

        LatLngBounds bounds = builder.build();
        int padding = 0; // offset from edges of the map in pixels
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

        googleMap.moveCamera(cu);

        ArrayList<Cluster> clusterList = Utils.cluster(markersList,
                (int) googleMap.getCameraPosition().zoom);

        // Removes all markers, overlays, and polylines from the map.
        googleMap.clear();

        // Zoom in, animating the camera.
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(previousZoomLevel),
                2000, null);

        CircleOptions circleOptions = new CircleOptions().center(point) //
                // setcenter
                .radius(3000) // set radius in meters
                .fillColor(Color.TRANSPARENT) // default
                .strokeColor(Color.BLUE).strokeWidth(5);

        googleMap.addCircle(circleOptions);

        for (Marker detail : markersList) {

            if (detail.getBhkTypeString().equalsIgnoreCase("1 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk1)));
            } else if (detail.getBhkTypeString().equalsIgnoreCase("2 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk_2)));

            }

            else if (detail.getBhkTypeString().equalsIgnoreCase("3 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk_3)));

            } else if (detail.getBhkTypeString().equalsIgnoreCase("2.5 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk2)));

            } else if (detail.getBhkTypeString().equalsIgnoreCase("4 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk_4)));

            } else if (detail.getBhkTypeString().equalsIgnoreCase("5 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk5)));

            } else if (detail.getBhkTypeString().equalsIgnoreCase("5+ BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk_5)));

            }

            else if (detail.getBhkTypeString().equalsIgnoreCase("2 BHK")) {
                googleMap.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(detail.getLatitude(), detail
                                        .getLongitude()))
                        .snippet(String.valueOf(""))
                        .title("Flat" + flatDetailsList.indexOf(detail))
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.bhk_2)));

            }
        }

        for (Cluster cluster : clusterList) {

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inMutable = true;
            options.inPurgeable = true;
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.cluster_marker, options);

            Canvas canvas = new Canvas(bitmap);

            Paint paint = new Paint();
            paint.setColor(getResources().getColor(R.color.white));
            paint.setTextSize(30);

            canvas.drawText(String.valueOf(cluster.getMarkerList().size()), 10,
                    40, paint);

            googleMap.addMarker(new MarkerOptions()
                    .position(
                            new LatLng(cluster.getClusterLatitude(), cluster
                                    .getClusterLongitude()))
                    .snippet(String.valueOf(cluster.getMarkerList().size()))
                    .title("Cluster")
                    .icon(BitmapDescriptorFactory.fromBitmap(bitmap)));

        }

    }
static int OFFSET=268435456;
静态双半径=85445659.4471;
静态双pi=3.1444;
公共静态双lonToX(双lon){
返回数学圆(偏移+半径*lon*pi/180);
}
公共静态双latToY(双lat){
返回整数(偏移量
-半径
*数学日志((1+Math.sin(lat*pi/180))
/(1-数学sin(lat*pi/180))/2);
}
公共静态整数像素距离(双lat1,双lon1,双lat2,
双lon2,整数缩放){
双x1=lonToX(lon1);
双y1=车床玩具(车床1);
双x2=lonToX(lon2);
双y2=拉特玩具(拉特2);
返回(整数)(数学)
.sqrt(数学功率((x1-x2),2)+数学功率((y1-y2),2))>>(21-zoom);
}
静态ArrayList群集(ArrayList标记,整数缩放){
ArrayList clusterList=新的ArrayList();
ArrayList originalListCopy=新ArrayList();
用于(标记:标记){
原始复印件。添加(标记);
}
/*循环,直到比较完所有标记*/
对于(int i=0;i0){
markerList.add(markers.get(i));
Cluster Cluster=新群集(clusterList.size(),markerList,
markerList.size()+1,originalListCopy.get(i)
.getLatitude(),originalListCopy.get(i)
.getLongitude());
clusterList.add(集群);
原件。删除(i);
删除(i);
i=0;
}否则{
i++;
}
/*如果已将标记添加到集群,请同时添加该标记*/
/*我们正在比较并从阵列中删除原始文件*/
}
返回集群列表;
}
只需在这里输入包含纬度和经度的数组列表,然后显示集群。下面是函数。
@凌驾
已完成的公用void ontask(ArrayList FlatDetails列表){
LatLngBounds.Builder=新的LatLngBounds.Builder();
originalListCopy=新的ArrayList();
ArrayList markersList=新建ArrayList();
用于(FlatDetails详细列表:flatDetailsList){
添加(新标记)(detailList.getLatitude(),detailList
.getLongitude(),detailList.getApartmentTypeString());
原始列表。添加(详细列表);
builder.include(新板条)(detailList.getLatitude(),detailList
.getLongitude());
}
LatLngBounds bounds=builder.build();
int padding=0;//与贴图边缘的偏移量(以像素为单位)
CameraUpdate cu=CameraUpdateFactory.newLatLngBounds(边界、填充);
谷歌地图。移动摄像机(cu);
ArrayList clusterList=Utils.cluster(markersList,
(int)googleMap.getCameraPosition().zoom);
//从地图中删除所有标记、覆盖和多段线。
googleMap.clear();
//放大,设置摄影机动画。
googleMap.animateCamera(CameraUpdateFactory.zoomTo(先前的ZoomLevel),
2000年,无效);
CircleOptions CircleOptions=新CircleOptions().中心(点)//
//设置中心
.半径(3000)//以米为单位设置半径
.fillColor(Color.TRANSPARENT)//默认值
.strokeColor(颜色.蓝色).strokeWidth(5);
googleMap.addCircle(圆圈选项);
用于(标记详细信息:标记列表){
if(detail.getBhkTypeString().equalsIgnoreCase(“1 BHK”)){
addMarker(新的MarkerOptions()
.职位(
新板条(detail.getLatitude(),detail
.getLongitude())
.snippet(String.valueOf(“”)
.标题(“平面”+平面详图列表索引(详图))
.图标(位图描述符工厂)
.fromResource(R.drawable.bhk1));
}else if(detail.getBhkTypeString().equalsIgnoreCase(“2 BHK”)){
addMarker(新的MarkerOptions()
.职位(
新板条(detail.getLatitude(),detail
.getLongitude())
.snippet(String.valueOf(“”)
.标题(“平面”+平面详图列表索引(详图))
.图标(位图描述符工厂)
.fromResource(R.drawable.bhk_2));
}
如果有的话(详情)