有没有可用来解析 KML 的库?
你将创建自己的库,但不需要编写任何代码。
我建议查看http://code.google.com/apis/kml/documentation/kmlreference.html。从那里可以获取XML Schema。一旦您获得了模式,您就可以使用JAXB生成一个对象树以轻松解析和编写KML文档。
这也可能是一个好资源,看起来其他人已经做过了!
public ArrayList<ArrayList<LatLng>> getCoordinateArrays() {
ArrayList<ArrayList<LatLng>> allTracks = new ArrayList<ArrayList<LatLng>>();
try {
StringBuilder buf = new StringBuilder();
InputStream json = MyApplication.getInstance().getAssets().open("track.kml");
BufferedReader in = new BufferedReader(new InputStreamReader(json));
String str;
String buffer;
while ((str = in.readLine()) != null) {
buf.append(str);
}
in.close();
String html = buf.toString();
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
ArrayList<String> tracksString = new ArrayList<String>();
for (Element e : doc.select("coordinates")) {
tracksString.add(e.toString().replace("<coordinates>", "").replace("</coordinates>", ""));
}
for (int i = 0; i < tracksString.size(); i++) {
ArrayList<LatLng> oneTrack = new ArrayList<LatLng>();
ArrayList<String> oneTrackString = new ArrayList<String>(Arrays.asList(tracksString.get(i).split("\\s+")));
for (int k = 1; k < oneTrackString.size(); k++) {
LatLng latLng = new LatLng(Double.parseDouble(oneTrackString.get(k).split(",")[0]),
Double.parseDouble(oneTrackString.get(k).split(",")[1]));
oneTrack.add(latLng);
}
allTracks.add(oneTrack);
}}
} catch (Exception e) {
e.printStackTrace();
}
return allTracks;
}
private static void readKML(InputStream fileKML, String nameCoordinates) {
String column = null;
Boolean folder = Boolean.TRUE;
Boolean placemark = Boolean.FALSE;
Boolean placeCorrect = Boolean.FALSE;
BufferedReader br = new BufferedReader(new InputStreamReader(fileKML));
try {
while ((column = br.readLine()) != null) {
if (folder) {
int ifolder = column.indexOf("<Folder>");
if (ifolder != -1) {
folder = Boolean.FALSE;
placemark = Boolean.TRUE;
continue;
}
}
if (placemark) {
String tmpLine = nameCoordinates;
tmpLine = tmpLine.replaceAll("\t", "");
tmpLine = tmpLine.replaceAll(" ", "");
String tmpColumn = column;
tmpColumn = tmpColumn.replaceAll("\t", "");
tmpColumn = tmpColumn.replaceAll(" ", "");
int name = tmpColumn.indexOf(tmpLine);
if (name != -1) {
placemark = Boolean.FALSE;
placeCorrect = Boolean.TRUE;
continue;
}
}
if (placeCorrect) {
int coordin = column.indexOf("<coordinates>");
if (coordin != -1) {
String tmpCoordin = column;
tmpCoordin = tmpCoordin.replaceAll(" ", "");
tmpCoordin = tmpCoordin.replaceAll("\t", "");
tmpCoordin = tmpCoordin.replaceAll("<coordinates>", "");
tmpCoordin = tmpCoordin
.replaceAll("</coordinates>", "");
String[] coo = tmpCoordin.split(",");
System.out.println("LONG: "+coo[0]);
System.out.println("LATI: "+coo[1])
break;
}
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return cresp;
}
由于它是xml,您可以使用任何解析器读取数据,但仍然有一个库可用于http://code.google.com/p/libkml/,它具有Java绑定,但该库是用C++编写的。
如果您使用的是Android Studio :)
dependencies {
compile 'org.jsoup:jsoup:1.8.1'
}
// find a way to read the file and store it in a string
String inputFileContents = "";
String xmlContent = inputFileContents;
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
for(Element e : doc.select("LineString").select("coordinates")) {
// the contents
System.out.println(e.text());
}
您可以进行多个select()方法调用。 我将代码简化为:
Element e = doc.select("LineString").select("coordinates").first();