引入依赖
<!--解析、生成kml文件类-->
<dependency>
<groupId>de.micromata.jak</groupId>
<artifactId>JavaAPIforKml</artifactId>
<version>2.2.0</version>
</dependency>
使用方法
注意:需要什么内容可自行添加
//生成线路路径kml文件并返回流给前端
public void exportCornerKml(List<Map<String,String>> cornerData, HttpServletResponse httpServletResponse) throws Exception{
//数据准备
OutputStream outputStream=null;
Kml kml = KmlFactory.createKml();
Document document = kml.createAndSetDocument().withName("").withId("").withVisibility(true).withOpen(true).withDescription("");
// 创建Style
Style style = document.createAndAddStyle().withId("normal_default");
LineStyle lineStyle = style.createAndSetLineStyle().withColor("ffffffff").withWidth(1);
// 创建Placemark
Placemark placemark = document.createAndAddPlacemark();
placemark.withName("和城-厉阳");
placemark.withVisibility(true).withDescription("");
placemark.withStyleUrl("#default");
// 创建LineString
LineString lineString = placemark.createAndSetLineString();
lineString.withExtrude(false).withTessellate(true).withAltitudeMode(AltitudeMode.CLAMP_TO_GROUND);
List<Coordinate> coordinates = new ArrayList<>();
for (int i=0;i<cornerData.size();i++){
coordinates.add(new Coordinate(Double.valueOf(cornerData.get(i).get("positionX")), Double.valueOf(cornerData.get(i).get("positionY")), 0.0));
}
lineString.setCoordinates(coordinates);
//输出
try {
//获取表并写入
outputStream=httpServletResponse.getOutputStream();
httpServletResponse.setContentType("application/vnd.google-earth.kml+xml");
httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("路径.kml", "UTF-8"));
kml.marshal(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}