首先查看最终效果
第一步:首先创建BmapPoint实体类 注意:经纬度的构造方法参数一定不要写反!!!!
import lombok.Data;
@Data
public class BmapPoint {
private double lng;// 经度
private double lat;// 纬度
public BmapPoint() {
}
public BmapPoint(double lng, double lat) {
this.lng = lng;
this.lat = lat;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BmapPoint) {
BmapPoint bmapPoint = (BmapPoint) obj;
return (bmapPoint.getLng() == lng && bmapPoint.getLat() == lat) ? true : false;
} else {
return false;
}
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
}
第二步:添加GraphUtils工具类(含测试)
import com.honghu.cloud.bean.envop.BmapPoint;
import java.util.Arrays;
public class GraphUtils {
/**
* 判断点是否在多边形内(基本思路是用交点法)
*
* @param point
* @param boundaryPoints
* @return
*/
public static boolean isPointInPolygon(BmapPoint point, BmapPoint[] boundaryPoints) {
// 防止第一个点与最后一个点相同
if (boundaryPoints != null && boundaryPoints.length > 0
&& boundaryPoints[boundaryPoints.length - 1].equals(boundaryPoints[0])) {
boundaryPoints = Arrays.copyOf(boundaryPoints, boundaryPoints.length - 1);
}