项目中需要通过一条线路,计算一个在辐射范围内包含该线路的图形。使用JTS包计算缓冲区即可得到。
<!-- jts依赖--> <dependency> <groupId>com.vividsolutions</groupId> <artifactId>jts-core</artifactId> <version>1.14.0</version> </dependency> <!--解析字符串--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.1</version> </dependency>
import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LineString; import java.util.List; /** * 获取缓冲区 */ public class Mytest { public static void main(String[] args) { String position = "[113.2136306762695,23.2724609375],[113.2136001586914,23.27313613891602],[113.2136001586914,23.27411651611328],[113.2136154174805,23.27562141418457],[113.2136154174805,23.27562141418457],[113.2136459350586,23.27606582641602],[113.2136459350586,23.27606582641602],[113.2151107788086,23.27617835998535],[113.2151107788086,23.27617835998535],[113.2170639038086,23.27627754211426],[113.2213287353516,23.27650451660156],[113.2219467163086,23.27655601501465]"; JSONObject jsonObject = JSONUtil.parseObj("{\"type\":\"LineString\",\"coordinates\":["+ position +"]}"); JSONArray jsonArray = jsonObject.getJSONArray("coordinates"); Coordinate coorArr[] = new Coordinate[jsonArray.size()]; //封装数据 for (int i = 0; i < jsonArray.size(); i++){ String pointArr[] = jsonArray.get(i).toString().replace("[", "").replace("]", "").split(","); if (pointArr != null && pointArr.length == 2){ Coordinate coord = new Coordinate(Double.parseDouble(pointArr[0]),Double.parseDouble(pointArr[1])); coorArr[i]=coord; } } GeometryFactory geoFac = new GeometryFactory(); LineString createLineString = geoFac.createLineString(coorArr); // 距离转换角度 = meter / (2*Math.PI*6371004)*360 //线路附近100米范围 Geometry buffer = createLineString.buffer(100 / (2*Math.PI*6371004)*360); //将结果封装返回 String subBetween = StrUtil.subBetween(buffer.toString(),"((", "))"); List<String> split = StrUtil.split(subBetween, ", "); String result = ""; for (String item: split) { List<String> split1 = StrUtil.split(item, " "); String temp = "["+ split1.get(0) +"," + split1.get(1) +"]" +","; result = result + temp; } System.out.println(result); } }
根据折线经纬度获取的折线平行线:https://www.cnblogs.com/seeyou1993/p/11474082.html
Turf.js:http://turfjs.org/docs/#buffer
高德地图轨迹回放:https://lbs.amap.com/demo/javascript-api/example/marker/replaying-historical-running-data