判断经纬度是否在圆圈内
/** * 判断经纬度是否在圆内 * * @param lon 目标经度 * @param lat 目标纬度 * @param dis 半径距离,米 * @param centerLon 圆心经度 * @param centerLat 圆心纬度 * @return */
//如果在的返回true ,如果不在返回false
public static boolean inCircle(Double lon, Double lat, Double dis, Double centerLon, Double centerLat) { double radLat1 = lat * Math.PI / 180.0; double radLat2 = centerLat * Math.PI / 180.0; double a = radLat1 - radLat2; double b = lon * Math.PI / 180.0 - centerLon * Math.PI / 180.0; double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); return s * 6378.137 * 1000 < dis; }
判断经纬度是否在多边形区域内
/** * 判断是否在多边形区域内 * * @param pointLon 要判断的点的纵坐标 经度 * @param pointLat 要判断的点的横坐标 纬度 * @param lon 区域各顶点的纵坐标数组 经度集合 * @param lat 区域各顶点的横坐标数组 纬度集合 * @return */ public static boolean isInPolygon(double pointLon, double pointLat, double[] lon, double[] lat) { // 将要判断的横纵坐标组成一个点 Point2D.Double point = new Point2D.Double(pointLon, pointLat); // 将区域各顶点的横纵坐标放到一个点集合里面 List<Point2D.Double> pointList = new ArrayList<Point2D.Double>(); double polygonPoint_x, polygonPoint_y; for (int i = 0; i < lon.length; i++) { polygonPoint_x = lon[i]; polygonPoint_y = lat[i]; Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y); pointList.add(polygonPoint); } return check(point, pointList); } /** * 一个点是否在多边形内 * * @param point 要判断的点的横纵坐标 * @param polygon 组成的顶点坐标集合 * @return */ private static boolean check(Point2D.Double point, List<Point2D.Double> polygon) { java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath(); Point2D.Double first = polygon.get(0); // 通过移动到指定坐标(以双精度指定),将一个点添加到路径中 peneralPath.moveTo(first.x, first.y); polygon.remove(0); for (Point2D.Double d : polygon) { // 通过绘制一条从当前坐标到新指定坐标(以双精度指定)的直线,将一个点添加到路径中。 peneralPath.lineTo(d.x, d.y); } // 将几何多边形封闭 peneralPath.lineTo(first.x, first.y); peneralPath.closePath(); // 测试指定的 Point2D 是否在 Shape 的边界内。 return peneralPath.contains(point); }