import com.alibaba.fastjson.JSON;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
public class GisUtils {
private static double EARTH_RADIUS = 6378.137;
private static double rad(double d) {
return d * Math.PI / 180.0;
}
private static double getDistance(double lat1, double lng1, double lat2,
double lng2) {
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
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)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000d) / 10000d;
return s * 1000;
}
/**
* 判断一个点是否在圆形区域内,单位米
*/
public static boolean isInCircle(String checkPoint,String circlePoint, String circleRadius) {
double lng = JSON.parseObject(checkPoint).getDouble("longitude");
double lat = JSON.parseObject(checkPoint).getDouble("latitude");
checkPoint = lng + "," + lat;
String[] split = checkPoint.split(",");
String[] split1 = circlePoint.split(",");
return getDistance(Double.valueOf(split[1].trim()), Double.valueOf(split[0].trim()), Double.valueOf(split1[1].trim()), Double.valueOf(split1[0].trim())) < Double.parseDouble(circleRadius);
}
public static boolean isInPolygon(String checkPoint,List<String> points) {
// 将要判断的横纵坐标组成一个点
double lng = JSON.parseObject(checkPoint).getDouble("longitude");
double lat = JSON.parseObject(checkPoint).getDouble("latitude");
checkPoint = lng + "," + lat;
String[] split1 = checkPoint.split(",");
Point2D.Double point1 = new Point2D.Double(Double.valueOf(split1[0].trim()), Double.valueOf(split1[1].trim()));
// 将区域各顶点的横纵坐标放到一个点集合里面
List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
double polygonPoint_x = 0.0, polygonPoint_y = 0.0;
for (String point : points) {
String[] split = point.split(",");
polygonPoint_x = Double.valueOf(split[0].trim());
polygonPoint_y = Double.valueOf(split[1].trim());
Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
pointList.add(polygonPoint);
}
return check(point1, 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);
}
}
也可以借助Mysql数据库进行判断,前提点的存储是Point类型
//判断在多边形内,返回1表示在,返回0表示不在
select ST_Intersects(ST_GeomFromText('Polygon((0 0,0 5,5 0,0 0))'),ST_GeomFromText('Point(5 5)'))
//判断在圆内,point是t_map保存点的字段,类型是Point,116.404 39.915是圆心坐标,500是半径,单位米
select
*
from
t_map
where
round(
(2 * asin(sqrt(pow(sin((ST_Y(point) * pi() / 180 - ST_Y(ST_GeomFromText('Point(116.404 39.915)')) * pi() / 180) / 2), 2) +
cos(ST_Y(point) * pi() / 180) * cos(ST_Y(ST_GeomFromText('Point(116.404 39.915)')) * pi() / 180) *
pow(sin((ST_X(point) * pi() / 180 - ST_X(ST_GeomFromText('Point(116.404 39.915)')) * pi() / 180) / 2), 2)
)
)
) * 6378.137 * 10000) / 10000 * 1000 < 500