|
@ -109,6 +109,25 @@ public class KmzParserUtil { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 处理当前Placemark的所有坐标,为每个坐标创建新的Placemark对象 |
|
|
|
|
|
* |
|
|
|
|
|
* @param currentPlacemark 当前Placemark对象 |
|
|
|
|
|
* @param currentCoordinates 当前Placemark的所有坐标 |
|
|
|
|
|
* @param placemarks 存储所有Placemark的列表 |
|
|
|
|
|
*/ |
|
|
|
|
|
private static void processCurrentPlacemarkCoordinates(Placemark currentPlacemark, List<String> currentCoordinates, List<Placemark> placemarks) { |
|
|
|
|
|
if (currentPlacemark != null && !currentCoordinates.isEmpty()) { |
|
|
|
|
|
for (String coords : currentCoordinates) { |
|
|
|
|
|
Placemark newPlacemark = new Placemark(); |
|
|
|
|
|
newPlacemark.setName(currentPlacemark.getName()); |
|
|
|
|
|
newPlacemark.setAttributes(new HashMap<>(currentPlacemark.getAttributes())); |
|
|
|
|
|
newPlacemark.setCoordinates(coords); |
|
|
|
|
|
placemarks.add(newPlacemark); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 解析KML文件内容 |
|
|
* 解析KML文件内容 |
|
|
* |
|
|
* |
|
@ -120,6 +139,7 @@ public class KmzParserUtil { |
|
|
Placemark currentPlacemark = null; |
|
|
Placemark currentPlacemark = null; |
|
|
boolean inDescription = false; |
|
|
boolean inDescription = false; |
|
|
StringBuilder descriptionContent = new StringBuilder(); |
|
|
StringBuilder descriptionContent = new StringBuilder(); |
|
|
|
|
|
List<String> currentCoordinates = new ArrayList<>(); |
|
|
|
|
|
|
|
|
// 添加调试日志
|
|
|
// 添加调试日志
|
|
|
System.out.println("开始解析KML内容,总行数: " + lines.length); |
|
|
System.out.println("开始解析KML内容,总行数: " + lines.length); |
|
@ -128,10 +148,10 @@ public class KmzParserUtil { |
|
|
line = line.trim(); |
|
|
line = line.trim(); |
|
|
|
|
|
|
|
|
if (line.startsWith("<Placemark>")) { |
|
|
if (line.startsWith("<Placemark>")) { |
|
|
if (currentPlacemark != null) { |
|
|
// 处理当前Placemark的所有坐标
|
|
|
placemarks.add(currentPlacemark); |
|
|
processCurrentPlacemarkCoordinates(currentPlacemark, currentCoordinates, placemarks); |
|
|
} |
|
|
|
|
|
currentPlacemark = new Placemark(); |
|
|
currentPlacemark = new Placemark(); |
|
|
|
|
|
currentCoordinates = new ArrayList<>(); |
|
|
System.out.println("发现新的Placemark"); |
|
|
System.out.println("发现新的Placemark"); |
|
|
} else if (line.startsWith("<name>")) { |
|
|
} else if (line.startsWith("<name>")) { |
|
|
if (currentPlacemark != null) { |
|
|
if (currentPlacemark != null) { |
|
@ -155,15 +175,14 @@ public class KmzParserUtil { |
|
|
} else if (line.startsWith("<coordinates>")) { |
|
|
} else if (line.startsWith("<coordinates>")) { |
|
|
if (currentPlacemark != null) { |
|
|
if (currentPlacemark != null) { |
|
|
String coords = line.replace("<coordinates>", "").replace("</coordinates>", "").trim(); |
|
|
String coords = line.replace("<coordinates>", "").replace("</coordinates>", "").trim(); |
|
|
currentPlacemark.setCoordinates(coords); |
|
|
currentCoordinates.add(coords); |
|
|
System.out.println("解析到坐标: " + coords); |
|
|
System.out.println("解析到坐标: " + coords); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (currentPlacemark != null) { |
|
|
// 处理最后一个Placemark
|
|
|
placemarks.add(currentPlacemark); |
|
|
processCurrentPlacemarkCoordinates(currentPlacemark, currentCoordinates, placemarks); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 打印解析结果
|
|
|
// 打印解析结果
|
|
|
System.out.println("解析完成,共找到 " + placemarks.size() + " 个地标"); |
|
|
System.out.println("解析完成,共找到 " + placemarks.size() + " 个地标"); |
|
|