计算给定的坐标的中心点坐标
private QueryEntity getCentPoint(List<QueryEntity> pointlist){ int total = pointlist.size(); double X = 0, Y = 0, Z = 0; for (QueryEntity g : pointlist) { double lat, lon, x, y, z; lat = (Double.parseDouble(g.getY())) * Math.PI / 180; //纬度 lon = (Double.parseDouble(g.getX())) * Math.PI / 180; //经度 x = Math.cos(lat) * Math.cos(lon); y = Math.cos(lat) * Math.sin(lon); z = Math.sin(lat); X += x; Y += y; Z += z; } X = X / total; Y = Y / total; Z = Z / total; double Lon = Math.atan2(Y, X); double Hyp = Math.sqrt(X * X + Y * Y); double Lat = Math.atan2(Z, Hyp); QueryEntity ql = new QueryEntity(); ql.setX(Double.toString(Lon * 180 / Math.PI)); ql.setY(Double.toString(Lat * 180 / Math.PI)); ql.setName("center"); return ql; }
计算算的点之间的距离在400km以内的可以使用简化的 代码
private QueryEntity getCentPoint(List<QueryEntity> pointlist){ int total = pointlist.size(); double lat, lon, x, y, z; for (QueryEntity g : pointlist) { lat += (Double.parseDouble(g.getY())) * Math.PI / 180; //纬度 lon += (Double.parseDouble(g.getX())) * Math.PI / 180; //经度 } lat /= total; lon /=total; QueryEntity ql = new QueryEntity(); ql.setX(Double.toString(Lon * 180 / Math.PI)); ql.setY(Double.toString(Lat * 180 / Math.PI)); ql.setName("center"); return ql; }