pom.xml依赖导包
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>?
点是否在园? 新建MapDrawCircleCurrency.class
使用? MapDrawCircleCurrency.inCircle(点经度,点纬度,园半径,圆心经度,圆心纬度)在园内返回true不在false
package com.yykj.system.currency;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
public class MapDrawCircleCurrency {
/**
* 判断经纬度是否在圆内
*
* @param lon 目标经度
* @param lat 目标纬度
* @param dis 距离,米
* @param centerLon 圆心经度
* @param centerLat 圆心纬度
* @return
*/
public static boolean inCircle(double lon, double lat, long dis, double centerLon, double centerLat) {
GlobalCoordinates source = new GlobalCoordinates(lat, lon);
GlobalCoordinates target = new GlobalCoordinates(centerLat, centerLon);
double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
Boolean p1 = false;
if (meter2 < dis) {
p1 = true;
}
return p1;
}
public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) {
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
return geoCurve.getEllipsoidalDistance();
}
}
点是否在多边形内(支持特殊多边形)
使用 MapDrawPolyGonCurrency.withinAndIntersects(多边形经纬度,点纬度,点经度)
注意 1.传入MapDrawPolyGonCurrency.withinAndIntersects的多边形经纬度格式 纬度|经度,纬度|经度(格式不对会报错)120.72325351783762,28.000006719726915|120.72222396274616,27.967331459803727|120.76126125996396,27.976202520995326|120.74324404586342,28.0023565306904|120.72325351783762,28.000006719726915
注意 2.传入MapDrawPolyGonCurrency.withinAndIntersects的多边形最后需拼接上第一个点的经纬度如
? ? ? ????????? 点1:111,111
????????? ? ? ? 点2:222,222
????????? ? ? ? 点3:333,333
????????? ? ? ? 点4:444,444
? ? ? ? ? ? ? ? 拼接 点1?:111,111
正确格式??111,111|222,222|333,333|444,444|111,111
?新建MapDrawPolyGonCurrency.class
package com.yykj.system.currency;
import org.locationtech.jts.geom.Coordinate;
public class MapDrawPolyGonCurrency {
public static boolean withinAndIntersects(String lnglatPolygon, double lat, double lng) {
Coordinate[] latLonMap = MapDrawPolygonCurrencyImpl.getCoordinateArray(lnglatPolygon);
Coordinate coordinate = new Coordinate(lng, lat);
boolean within = MapDrawPolygonCurrencyImpl.withinAndIntersects(latLonMap, coordinate);
return within;
}
}
?新建MapDrawPolygonCurrencyImpl.class
package com.yykj.system.currency;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
public class MapDrawPolygonCurrencyImpl {
/**
* 将字符串多坐标点转换为坐标数组
*
* @param latLonString "116.616634,40.272665|116.644733,40.280371|116.636181,40.264352|116.616634,40.272665"
* @return org.locationtech.jts.geom.Coordinate[]
* @author guochao.bj@fang.com
* @date
*/
public static Coordinate[] getCoordinateArray(String latLonString) {
String[] split = latLonString.split("\\|");
Coordinate[] coordinateArray = new Coordinate[split.length];
for (int i = 0; i < split.length; i++) {
String[] LatLng = split[i].split(",");
Coordinate coordinate = new Coordinate(Double.valueOf(LatLng[0]), Double.valueOf(LatLng[1]));
coordinateArray[i] = coordinate;
}
return coordinateArray;
}
/**
* 判断坐标点是否在多坐标点组成的多边形面内
*
* @param coordinateArray
* @param coordinate
* @return boolean
* @author guochao.bj@fang.com
* @date
* within 判断是否在内部,边缘点返回false
* intersects 判断是否相交,弥补 within边缘点缺陷,
*/
public static boolean withinAndIntersects(Coordinate[] coordinateArray, Coordinate coordinate) {
boolean result = false;
//小于3个点无法组成多边形
if (coordinateArray.length < 3) {
return result;
}
//必须首尾坐标点相同组成闭合多边形
if (!(coordinateArray[0].equals2D(coordinateArray[coordinateArray.length - 1]))) {
return result;
}
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(coordinate);
Polygon polygon = geometryFactory.createPolygon(coordinateArray);
if (point.within(polygon) || point.intersects(polygon)) {
result = true;
}
return result;
}
}
|