如何在Andorid中读取Google Map的kmz文件

时间:2018-12-05 05:31:09

标签: android google-maps kmz

嗨,我有KMZ文件,我想解析该KMZ文件,以便我可以读取尝试使用KmlLayer的文件的数据,但是没有得到任何帮助,这是我的代码< / p>

InputStream inputStream = new FileInputStream(path);
KmlLayer layer = new KmlLayer(mMap, inputStream, getApplicationContext());
layer.addLayerToMap();

但是在创建任何解决方案的KmlLayer对象时,我都遇到了解析异常。

1 个答案:

答案 0 :(得分:1)

由于KMZ已压缩KML,因此您应先unzip.kmz文件.kmlcreateLayerFromKmz()或使用ZipInputStream而不是{{3 }},就像这种private KmlLayer createLayerFromKmz(String kmzFileName) { KmlLayer kmlLayer = null; InputStream inputStream; ZipInputStream zipInputStream; try { inputStream = new FileInputStream(kmzFileName); zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream)); ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { String fileName = zipEntry.getName(); if (fileName.endsWith(".kml")) { kmlLayer = new KmlLayer(mGoogleMap, zipInputStream, getApplicationContext()); } } zipInputStream.closeEntry(); } zipInputStream.close(); } catch(IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return kmlLayer; } 方法一样:

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;

    // path to your kmz file 
    String kmzFileName = Environment.getExternalStorageDirectory() + "/KMZ/markers.kmz";
    try {
        KmlLayer kmlLayer = createFromKmz(kmzFileName);
        kmlLayer.addLayerToMap();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }

}

您可以使用它,例如这样:

createLayerFromKmz()

NB! {{1}}仅适用于“扁平” KMZ结构。