KML文件解析显示在地图

您所在的位置:网站首页 kml文件是什么文件怎样打开 KML文件解析显示在地图

KML文件解析显示在地图

2024-07-13 08:11:03| 来源: 网络整理| 查看: 265

风景 KML解析用到的工具jar包下载 dom4j.jar

KML文件格式1: kml演示.kml normal #s_ylw-pushpin highlight #s_ylw-pushpin_hl 1.1 http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png ff7f00ff 00ffffff 1.3 http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png ff7f00ff 00ffffff 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 http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png ffffffff 0.6 ff000000 2.0 112.89506455,28.05539774,0 1 http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png ffffffff 0.6 ff000000 2.0 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; }


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭