主函数:
public void testArea() {
//测试数据
String str="[[[119.14270520210266,36.707478761672974],[119.14281249046326,36.70579433441162],[119.15120244026184,36.70562267303467],[119.15090203285217,36.710225343704224],[119.14961457252502,36.710193157196045],[119.14967894554138,36.70814394950867],[119.14833784103394,36.708219051361084],[119.14833784103394,36.707704067230225],[119.1471254825592,36.707704067230225],[119.14710402488708,36.70858383178711],[119.14643883705139,36.70858383178711],[119.14641737937927,36.706953048706055],[119.14404630661011,36.706963777542114],[119.14404630661011,36.70748949050903],[119.14270520210266,36.707478761672974]]],[[[119.15127754211426,36.70525789260864],[119.1426944732666,36.70549392700195],[119.15151357650757,36.702189445495605],[119.15127754211426,36.70525789260864]]]";
//将字符串转换为二维数组
String[] strArray=str.replaceAll("\\[","").replaceAll("]","").split(",");
Location location = null;
List<Location> locationList = new ArrayList<>();
//循环添加元素
for (int i = 0; i < strArray.length; i += 2) {
//将double类型转换成字符串格式
double lon = Double.parseDouble(strArray[i]);//经度
double lat = Double.parseDouble(strArray[i+1]);//纬度
//保存经纬度
location = new Location();
location.setLon(lon);
location.setLat(lat);
//添加到List集合
locationList.add(location);
}
//计算
double area = PolygonAreaUtil.planarPolygonAreaMeters2(locationList);
//通过使用BigDecimal 对象进行保留两位小数
BigDecimal bigDecimal = new BigDecimal(area/1000000);
double ans_2 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(ans_2);
}
实体类
import lombok.Data;
import java.math.BigDecimal;
@Data
public class Location {
private double lon;
private double lat;
}
工具类
public static double planarPolygonAreaMeters2(List<Location> pointList) {
double earthRadiusMeters = 6371006.0;//地球半径
double metersPerDegree = 2.0 * Math.PI * earthRadiusMeters / 360.0; /// 米每度
double radiansPerDegree = Math.PI / 180.0;//弧度每度
double a = 0;
for (int i = 0; i < pointList.size(); i++) {
int j = (i + 1) % pointList.size();
Location point_i = pointList.get(i);
double xi = point_i.getLon() * metersPerDegree * Math.cos(point_i.getLat() * radiansPerDegree);
double yi = point_i.getLat() * metersPerDegree;
Location point_j = pointList.get(j);
double xj = point_j.getLon() * metersPerDegree * Math.cos(point_j.getLat() * radiansPerDegree);
double yj = point_j.getLat() * metersPerDegree;
a += xi * yj - xj * yi;
}
// 转化为正数
return Math.abs(a / 2);
}