KML解析用到的工具jar包下载 dom4j.jar
KML文件格式1:
kml演示.kml
normal
#s_ylw-pushpin
highlight
#s_ylw-pushpin_hl
kml演示
kml demo
#m_ylw-pushpin
1
1
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
KML文件格式2:
OvitalMap_20181122_221353
岳麓区2018年枯死松木伐除试点坐标.kmz
枯死木伐除试点
枯死木伐除试点
1
112.89506455,28.05539774,0
1
112.84106543,28.05342430,0
解析代码
/**
$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 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 mKmlFileElementBeanList = null;
public interface KmlHelperListener {
void onSuccessPoint(List 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内容 解析
* @param xml
*/
public Boolean parseXmlWithDom4j(String xml, KmlHelperListener mKmlHelperListener) throws Exception {
Log.e("kml", xml);
SAXReader reader = new SAXReader();
Document document = null;
try {
//读取xml字符串,注意这里要转成输入流
document = reader.read(new ByteArrayInputStream(xml.getBytes("utf-8")));
// document = reader.read(input);
//读取xml字符串,注意这里要转成输入流
Element root = document.getRootElement();
//读取xml字符串,注意这里要转成输入流
Element node = root.element("Document");
mKmlFileElementBeanList = new ArrayList<>();
Log.e("节点", node.getName());//Document
//使用递归
Iterator 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;
}
//遍历当前节点下的所有节点
public void listNodes(Element node, KmlHelperListener mKmlHelperListener) {
//根节点名
//Placemark节点中的name属性
try {
//如果当前节点是Placemark就解析其子节点
Log.e("根节点名字1", node.getName());
if ("Placemark".equals(node.getName())) {
KMLFileElementBean kmlFileElementBean = new KMLFileElementBean();
String name = node.elementText("name");
kmlFileElementBean.setName(name);
List coordinateList = new ArrayList<>();
Iterator iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
if ("Polygon".equals(e.getName())) {
Log.e("面名字3", e.getName());
kmlFileElementBean.setType("Polygon");
//遍历Point节点的所有子节点
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文件格式出现异常");
e.printStackTrace();
}
//同时迭代当前节点下面的所有子节点
Iterator iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
listNodes(e, mKmlHelperListener);
}
}
/**
* 根据类型 转换 坐标
* GPS转高德坐标
*/
public LatLng convert(LatLng sourceLatLng) {
CoordinateConverter converter = new CoordinateConverter(mContext);
// CoordType.GPS 待转换坐标类型
converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待转换坐标点
converter.coord(sourceLatLng);
// 执行转换操作
LatLng desLatLng = converter.convert();
return desLatLng;
}
}
/**
* 读取文件内容
*
* @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(); // 取内存中保存的数据
result = new String(data, "utf-8");
bos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}