根据polygon(经纬度点集合)计算polygon区域中心点经纬度

1. 计算中心点的java代码
public static void main(String[] args){
     
        String data = "110.669982,20.911926;110.607118,20.964672;110.585898,20.977463;110.576281,21.010324;110.561774,21.083386;110.471418,21.08862;110.420503,21.134889;110.395531,21.12548;110.373566,21.140111;110.356134,21.174582;110.337863,21.182542;110.345038,21.212646;110.35687,21.224833;110.358048,21.237502;110.355802,21.238662;110.35458,21.240839;110.352781,21.242121;110.350877,21.240554;110.349895,21.239792;110.348501,21.240987;110.3462,21.240853;110.344746,21.242339;110.346601,21.245187;110.353695,21.257104;110.354065,21.265279;110.351898,21.266447;110.353419,21.268715;110.353564,21.271738;110.351562,21.273307;110.34816,21.275408;110.34605,21.276378;110.344423,21.276628;110.342182,21.282489;110.34395,21.284781;110.34354,21.290655;110.346171,21.294658;110.346923,21.297623;110.343852,21.298103;110.340636,21.298592;110.337767,21.29943;110.337022,21.300861;110.335753,21.300208;110.331587,21.302726;110.334838,21.303866;110.335336,21.305399;110.33719,21.308289;110.336092,21.311139;110.334095,21.307851;110.33163,21.305436;110.327627,21.306776;110.324233,21.311276;110.32114,21.312898;110.318056,21.312797;110.313899,21.314587;110.31112,21.31822;110.307566,21.322719;110.304023,21.324917;110.300944,21.324669;110.298633,21.321385;110.296485,21.315804;110.293568,21.310944;110.290038,21.307521;110.285278,21.307663;110.28328,21.30521;110.281909,21.302627;110.279768,21.299597;110.275781,21.299421;110.273174,21.298966;110.271037,21.297215;110.267672,21.29588;110.26369,21.298256;110.259254,21.30191;110.256496,21.305453;110.254318,21.306764;110.251142,21.306791;110.246102,21.305138;110.2409,21.304353;110.237693,21.302311;110.237399,21.298712;110.237555,21.294417;110.236027,21.291527;110.233435,21.2885;110.231907,21.282609;110.230841,21.274562;110.231304,21.268967;110.231155,21.262508;110.2287,21.258193;110.225795,21.256762;110.222266,21.257201;110.219049,21.25794;110.215063,21.255682;110.211228,21.252136;110.20862,21.248875;110.203396,21.243645;110.19956,21.242848;110.19603,21.241912;110.194186,21.240223;110.192348,21.235515;110.19342,21.229611;110.198331,21.225215;110.204777,21.223087;110.206779,21.220904;110.207702,21.215862;110.20755,21.209985;110.205251,21.207583;110.201113,21.202914;110.19437,21.197014;110.187677,21.1898;110.193374,21.175421;110.205384,21.178479;110.216199,21.170838;110.228575,21.170744;110.22104,21.162358;110.230089,21.156123;110.224943,21.147663;110.233192,21.145868;110.230667,21.13976;110.231919,21.134278;110.220356,21.125001;110.212077,21.118616;110.198116,21.112534;110.191525,21.115383;110.190045,21.118501;110.187228,21.11991;110.185698,21.124238;110.18355,21.128868;110.17834,21.130947;110.174208,21.133571;110.171453,21.136893;110.169311,21.137337;110.166712,21.134902;110.163649,21.132883;110.158534,21.130989;110.155459,21.127946;110.153723,21.125647;110.15296,21.123485;110.148232,21.120562;110.141671,21.117591;110.138775,21.114097;110.136341,21.109033;110.135889,21.101553;110.138332,21.095572;110.141387,21.090019;110.14247,21.08128;110.14203,21.071376;110.140979,21.066622;110.141134,21.063898;110.144646,21.060646;110.145264,21.057217;110.143445,21.052738;110.13934,21.045926;110.134473,21.035797;110.131433,21.028849;110.131898,21.020388;110.137382,21.019475;110.141496,21.017105;110.143634,21.012975;110.147505,21.009226;110.151222,21.010927;110.15598,21.010977;110.1601,21.00841;110.164235,21.003734;110.165005,20.99785;110.164117,20.99182;110.177804,20.985182;110.208948,20.93909;110.362691,20.959437;110.449348,20.947815;110.495203,20.901978;110.616422,20.858425";
        String[] datas = data.split(";");
        List<double[]> points = new ArrayList<>();
        for (String str : datas) {
     
            String[] ss = str.split(",");
            double[] point = new double[2];
            point[0] = Double.valueOf(ss[0]);
            point[1] = Double.valueOf(ss[1]);
            points.add(point);
        }
        String centerPoint = getCenterPointFromListOfCoordinates(points);
        System.out.println(centerPoint);
    }

    /**
     * 中心点核心计算方法
     * @param coordinateList 经纬度点集合
     * @return
     */
    private static String getCenterPointFromListOfCoordinates(List<double[]> coordinateList) {
     
        int total = coordinateList.size();
        double X = 0;
        double Y = 0;
        double Z = 0;
        for (double[] coordinate : coordinateList) {
     
            double lat = coordinate[1] * Math.PI / 180;
            double lon = coordinate[0] * Math.PI / 180;
            X += Math.cos(lat) * Math.cos(lon);
            Y += Math.cos(lat) * Math.sin(lon);
            Z += Math.sin(lat);
        }
        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double lon2 = Math.atan2(Y, X);
        double hyp = Math.sqrt(X * X + Y * Y);
        double lat2 = Math.atan2(Z, hyp);
        double longitude = lon2 * 180 / Math.PI;
        double latitude = lat2 * 180 / Math.PI;
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMinimumFractionDigits(6);
        return nf.format(longitude) + "," + nf.format(latitude);
    }
2. 在百度地图中验证结果

① 运行第1步中的代码会得到中心点坐标110.264087,21.190308;
② 访问百度百度地图拾取坐标系统[http://api.map.baidu.com/lbsapi/getpoint/index.html]
并选中坐标反查
③ 输入运行结果坐标点110.264087,21.190308;
根据polygon(经纬度点集合)计算polygon区域中心点经纬度_第1张图片

你可能感兴趣的:(gis,gis,定位)