java实现地图围栏功能

/**
* 判断是否在多边形区域内
*
* @param pointLon
* 要判断的点的纵坐标 经度
* @param pointLat
* 要判断的点的横坐标 纬度
* @param lon
* 区域各顶点的纵坐标数组 经度集合
* @param lat
* 区域各顶点的横坐标数组 纬度集合
* @return
*/
public static boolean isInPolygon(double pointLon, double pointLat,List lon,List lat) {// List lon,List lat
// 将要判断的横纵坐标组成一个点
Point2D.Double point = new Point2D.Double(pointLon, pointLat);
// 将区域各顶点的横纵坐标放到一个点集合里面
List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
double polygonPoint_x = 0.0, polygonPoint_y = 0.0;
for (int i = 0; i < lon.size(); i++) {
//System.out.println(lon.get(i));
//System.out.println(lat.get(i));
polygonPoint_x = lon.get(i);
polygonPoint_y = lat.get(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);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值