美文网首页
KML文件解析显示在地图

KML文件解析显示在地图

作者: 未聞椛洺 | 来源:发表于2018-12-07 13:57 被阅读0次
风景
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>岳麓区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内容 解析
      *  @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<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;
}

//遍历当前节点下的所有节点
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<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节点的所有子节点
                    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<Element> 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;
}

相关文章

网友评论

      本文标题:KML文件解析显示在地图

      本文链接:https://www.haomeiwen.com/subject/cudthqtx.html