KML解析用到的工具jar包下載 dom4j.jar
KML文件格式1:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>kml演示.kml</name>
<StyleMap id="m_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#s_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#s_ylw-pushpin_hl</styleUrl>
</Pair>
</StyleMap>
<Style id="s_ylw-pushpin">
<IconStyle>
<scale>1.1</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<LineStyle>
<color>ff7f00ff</color>
</LineStyle>
<PolyStyle>
<color>00ffffff</color>
</PolyStyle>
</Style>
<Style id="s_ylw-pushpin_hl">
<IconStyle>
<scale>1.3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<LineStyle>
<color>ff7f00ff</color>
</LineStyle>
<PolyStyle>
<color>00ffffff</color>
</PolyStyle>
</Style>
<Placemark>
<name>kml演示</name>
<description>kml demo</description>
<styleUrl>#m_ylw-pushpin</styleUrl>
<gx:balloonVisibility>1</gx:balloonVisibility>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
112.9534054145206,28.17429329464148,0 112.9535870388715,28.17077100753803,0 112.9543308168626,28.16807084919302,0 112.9552953925895,28.16843727493238,0 112.9559857751675,28.17013030508299,0 112.9564530523403,28.17275841706835,0 112.9567491514611,28.17512130783434,0 112.9570255033991,28.17814122959389,0 112.9537821602722,28.178221949954,0 112.9534054145206,28.17429329464148,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
KML文件格式2:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:gx="http://www.google.com/kml/ext/2.2" >
<Document>
<name>OvitalMap_20181122_221353</name>
<Folder>
<name>岳麓區(qū)2018年枯死松木伐除試點坐標.kmz</name>
<Folder>
<name>枯死木伐除試點</name>
<Folder>
<name>枯死木伐除試點</name>
<Placemark>
<name>1</name>
<Style>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<color>ffffffff</color>
<scale>0.6</scale>
</IconStyle>
<LabelStyle>
<color>ff000000</color>
<scale>2.0</scale>
</LabelStyle>
</Style>
<Point>
<coordinates>112.89506455,28.05539774,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>1</name>
<Style>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<color>ffffffff</color>
<scale>0.6</scale>
</IconStyle>
<LabelStyle>
<color>ff000000</color>
<scale>2.0</scale>
</LabelStyle>
</Style>
<Point>
<coordinates>112.84106543,28.05342430,0</coordinates>
</Point>
</Placemark>
<Placemark>
</Folder>
</Folder>
</Folder>
</Document>
</kml>
解析代碼
/**
$activityName
@author LiuTao
@date 2018/11/23/023
*/
public class KmlHelper {
public static boolean addSampleSuccess = false; //判斷讀取KML是否成功
private Coordinate coordinate = null; //存儲從KML文件中讀取出來的坐標值和name
// private static List<LatLng> coordinateList = null;//存儲每次實例化的
Coordinate對象鹅颊,每個Coordinate都保存著不同的x,y,name
private Context mContext;
private AMap aMap;
private Polygon mPolygon;
private String kmlPath = "/storage/emulated/0/Android/kml演示.kml";
private KmlHelperListener mKmlHelperListener;
private List<KMLFileElementBean> mKmlFileElementBeanList = null;
public interface KmlHelperListener {
void onSuccessPoint(List<KMLFileElementBean> fileElementBeans);
}
public KmlHelper(Context context, AMap aMap) {
this.mContext = context;
this.aMap = aMap;
}
/**
* ZIP
*
* @param mKmlHelperListener
* @throws Exception
*/
public void parseKmlFile(String kmzPath, KmlHelperListener mKmlHelperListener) throws Exception {
File file = new File(kmzPath);//pathName為KML文件的路徑
try {
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInputStream = null;
InputStream inputStream = null;
ZipEntry entry = null;
zipInputStream = new ZipInputStream(new FileInputStream(file));
while ((entry = zipInputStream.getNextEntry()) != null) {
String zipEntryName = entry.getName().toLowerCase();
if (zipEntryName.endsWith("kml") || zipEntryName.endsWith("kmz")) {
inputStream = zipFile.getInputStream(entry);
//parseXmlWithDom4j(inputStream);
} else if (zipEntryName.endsWith("png")) {
}
}
zipInputStream.close();
inputStream.close();
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* kml
* Assets
* @param name
* @param mKmlHelperListener
*/
public void parseKml(String name, KmlHelperListener mKmlHelperListener) {
InputStream is = null;
try {
is = mContext.getAssets().open(name);
if (is != null) {
int lenght = is.available();
byte[] buffer = new byte[lenght];
is.read(buffer);
String result = new String(buffer, "utf8");
Log.e("kml", result);
parseXmlWithDom4j(result, mKmlHelperListener);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/***
* 讀取KML內(nèi)容 解析
* @param xml
*/
public Boolean parseXmlWithDom4j(String xml, KmlHelperListener mKmlHelperListener) throws Exception {
Log.e("kml", xml);
SAXReader reader = new SAXReader();
Document document = null;
try {
//讀取xml字符串漫雕,注意這里要轉(zhuǎn)成輸入流
document = reader.read(new ByteArrayInputStream(xml.getBytes("utf-8")));
// document = reader.read(input);
//讀取xml字符串慢睡,注意這里要轉(zhuǎn)成輸入流
Element root = document.getRootElement();
//讀取xml字符串砾脑,注意這里要轉(zhuǎn)成輸入流
Element node = root.element("Document");
mKmlFileElementBeanList = new ArrayList<>();
Log.e("節(jié)點", node.getName());//Document
//使用遞歸
Iterator<Element> iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
listNodes(e, mKmlHelperListener);
}
// listNodes(node, mKmlHelperListener);
addSampleSuccess = true;
mKmlHelperListener.onSuccessPoint(mKmlFileElementBeanList);
} catch (DocumentException e) {
// TODO: handle exception
e.printStackTrace();
}
return addSampleSuccess;
}
//遍歷當前節(jié)點下的所有節(jié)點
public void listNodes(Element node, KmlHelperListener mKmlHelperListener) {
//根節(jié)點名
//Placemark節(jié)點中的name屬性
try {
//如果當前節(jié)點是Placemark就解析其子節(jié)點
Log.e("根節(jié)點名字1", node.getName());
if ("Placemark".equals(node.getName())) {
KMLFileElementBean kmlFileElementBean = new KMLFileElementBean();
String name = node.elementText("name");
kmlFileElementBean.setName(name);
List<LatLng> coordinateList = new ArrayList<>();
Iterator<Element> iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
if ("Polygon".equals(e.getName())) {
Log.e("面名字3", e.getName());
kmlFileElementBean.setType("Polygon");
//遍歷Point節(jié)點的所有子節(jié)點
Element i1 = node.element("Polygon");
Element i2 = i1.element("outerBoundaryIs");
Element i3 = i2.element("LinearRing");
String nodeContent = i3.elementText("coordinates");
String[] nodeContentSplit = nodeContent.trim().split(" ");
for (int i = 0; i < nodeContentSplit.length; i++) {
String[] coorDinats = nodeContentSplit[i].split(",");
LatLng latLng = new LatLng(Double.parseDouble(coorDinats[1]), Double.parseDouble(coorDinats[0]));
coordinateList.add(convert(latLng));
}
kmlFileElementBean.setLatLngs(coordinateList);
} else if ("Point".equals(e.getName())) {
Log.e("點名字3", e.getName());
Element i1 = node.element("Point");
kmlFileElementBean.setType("Point");
String nodeContent = i1.elementText("coordinates");
String[] coorDinats = nodeContent.trim().split(",");
LatLng latLng = new LatLng(Double.parseDouble(coorDinats[1]), Double.parseDouble(coorDinats[0]));
coordinateList.add(convert(latLng));
kmlFileElementBean.setLatLngs(coordinateList);
}
}
mKmlFileElementBeanList.add(kmlFileElementBean);
}
} catch (Exception e) {
ToastUtils.showToast("讀取kml文件格式出現(xiàn)異常");
e.printStackTrace();
}
//同時迭代當前節(jié)點下面的所有子節(jié)點
Iterator<Element> iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
listNodes(e, mKmlHelperListener);
}
}
/**
* 根據(jù)類型 轉(zhuǎn)換 坐標
* GPS轉(zhuǎn)高德坐標
*/
public LatLng convert(LatLng sourceLatLng) {
CoordinateConverter converter = new CoordinateConverter(mContext);
// CoordType.GPS 待轉(zhuǎn)換坐標類型
converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待轉(zhuǎn)換坐標點
converter.coord(sourceLatLng);
// 執(zhí)行轉(zhuǎn)換操作
LatLng desLatLng = converter.convert();
return desLatLng;
}
}
/**
* 讀取文件內(nèi)容
*
* @return
*/
public static String readtext(File f) {
if (!f.exists()) {
return null;
}
FileInputStream is;
String result = null;
try {
is = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] array = new byte[1024];
int len = -1;
while ((len = is.read(array)) > 0 - 1) {
bos.write(array, 0, len);
}
byte[] data = bos.toByteArray(); // 取內(nèi)存中保存的數(shù)據(jù)
result = new String(data, "utf-8");
bos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}