根据经纬度坐标点返回所在行政区域实现
项目工程源码下载
行政区域数据下载
public Result getPosition (Double longitude, Double latitude) {
List<Center> centers = this.regionsService.getAllDistrict();
centers.sort(new Comparator<Center>() {
@Override
public int compare(Center o1, Center o2) {
String point1 = (String) o1.getAdcode();
Double lon1 = Double.valueOf(point1.split(",")[0]);
Double lat1 = Double.valueOf(point1.split(",")[1]);
Double dis1 = CalulateTwoLanLon.getDistance(lat1, lon1, latitude, longitude);
String point2 = (String) o2.getAdcode();
Double lon2 = Double.valueOf(point2.split(",")[0]);
Double lat2 = Double.valueOf(point2.split(",")[1]);
Double dis2 = CalulateTwoLanLon.getDistance(lat2, lon2, latitude, longitude);
if (dis1 < dis2)
return -1;
else if (dis1 > dis2)
return 1;
else
return 0;
}
});
String address = null;
for (Center center : centers) {
String point1 = (String) center.getAdcode();
Double lon1 = Double.valueOf(point1.split(",")[0]);
Double lat1 = Double.valueOf(point1.split(",")[1]);
Double dis1 = CalulateTwoLanLon.getDistance(lat1, lon1, latitude, longitude);
System.out.println(center.getId() + " " + dis1);
Integer id = center.getId();
Region region = this.regionsService.findById(id);
if(StringUtils.isEmpty(region.getPolyline()))
continue;
String[] polylines = region.getPolyline().split("\\|");
for (int i = 0; i < polylines.length; i++) {
if(Profile.isInPolygon(longitude, latitude, polylines[i])) {
address = this.regionsService.getFullNameById(id);
}
}
if(!StringUtils.isEmpty(address))
break;
}
return new Result(Result.SUCCESS, "响应成功!", address);
}
public class Profile {
public static boolean isInPolygon(Double pointLon, Double pointLat, String polyline) {
String[] points = polyline.split(";");
double[] lon = new double[points.length];
double[] lat = new double[points.length];
for (int i = 0; i < points.length; i++) {
String[] point = points[i].split(",");
lon[i] = Double.valueOf(point[0]);
lat[i] = Double.valueOf(point[1]);
}
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.length; i++) {
polygonPoint_x = lon[i];
polygonPoint_y = lat[i];
Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
pointList.add(polygonPoint);
}
return check(point, pointList);
}
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();
return peneralPath.contains(point);
}
}
public class CalulateTwoLanLon {
private static final double EARTH_RADIUS = 6378.137;
private static double rad(double d) {
return d * Math.PI / 180.0;
}
public 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 * 10000) / 10000;
return s;
}
}