地图(高德)判断某一点位是否位于某一城市/地区/行政区划内
这里以高德地图示例,其他地图思路是类似的
调用API(可以申请api选择web服务获取key)
https://restapi.amap.com/v3/config/district?key=yourAppKey&keywords=330500&extensions=all
传入需要的行政区域代码,得到如下数据,得到polyline是该地区的经纬度范围
用类似的思路也可以自定义范围来判断
Polygon
和 MultiPolygon
是用来表示地理空间数据的重要几何类型,选择需要的使用
import org.locationtech.jts.algorithm.PointLocator;
import org.locationtech.jts.geom.*;
/**
* 高德地图判断点位是否在区域内
*/
public class AMapUtils {
// 湖州市 https://restapi.amap.com/v3/config/district?key=yourAppKey&keywords=330500&extensions=all
public static final String hz_330500_polyline = "119.35233,30.421692;119.352496,30.422054;119.352971,30.424851;119.35268,30.425771;119.352023,30.427157;119.3508,30.429113;119.349119,30.432065;119.348813,30.433614;119.349692,30.438824;119.348722,30.440093;119.348386,30.440561;119.348133,30.440734;119.345771,30.442303;119.343669......注意经纬度要首尾相连";
public static boolean pointIsInHuZhou(double longitude, double latitude) {
//因为边界信息里面包含;和|分隔符,都需要切割
String[] hzpoints = hz_330500_polyline.split(";|\\|");
// 创建GeometryFactory
GeometryFactory geometryFactory = new GeometryFactory();
// 创建点坐标
Coordinate pointCoordinate = new Coordinate(longitude, latitude);
Coordinate[] coordinates = new Coordinate[hzpoints.length + 1];
//起始经纬度
double longitudeF = Double.parseDouble(hzpoints[0].split(",")[0]); // 经度
double latitudeF = Double.parseDouble(hzpoints[0].split(",")[1]); // 纬度
coordinates[0] = new Coordinate(longitudeF, latitudeF);
// 遍历多边形Map,查找包含点的多边形
for (int i = 1; i < hzpoints.length; i++) {
double longitude1 = Double.parseDouble(hzpoints[i].split(",")[0]); // 经度
double latitude1 = Double.parseDouble(hzpoints[i].split(",")[1]); // 纬度
coordinates[i] = new Coordinate(longitude1, latitude1);
}
// 一定要首尾相连,不然就会报错
coordinates[hzpoints.length] = new Coordinate(longitudeF, latitudeF);
return multiPolygonProcess(geometryFactory, coordinates, pointCoordinate);
}
/**
* 通过MultiPolygon判断
*
* @param geometryFactory
* @param coordinates
* @param pointCoordinate
* @return
*/
private static boolean multiPolygonProcess(GeometryFactory geometryFactory, Coordinate[] coordinates, Coordinate pointCoordinate) {
try {
// 创建MultiPolygon对象
MultiPolygon multiPolygon = geometryFactory
.createMultiPolygon(new Polygon[]{new GeometryFactory().createPolygon(coordinates)});
// 使用PointLocator检查点是否在多边形内
PointLocator pointLocator = new PointLocator();
int location = pointLocator.locate(pointCoordinate, multiPolygon);
return location != Location.EXTERIOR;
} catch (Exception e) {
System.out.println("异常原因:" + e);
}
return false;
}
/**
* 通过Polygon判断
*
* @param geometryFactory
* @param coordinates
* @param pointCoordinate
* @return
*/
private static boolean polygonProcess(GeometryFactory geometryFactory, Coordinate[] coordinates, Coordinate pointCoordinate) {
try {
LinearRing ring = geometryFactory.createLinearRing(coordinates);
Polygon polygon = geometryFactory.createPolygon(ring, null);
Point point = geometryFactory.createPoint(pointCoordinate); // 待判断的点
return point.within(polygon);
} catch (Exception e) {
System.out.println("异常原因:" + e);
}
return false;
}
}