如何在Android中读取Google Map的kmz文件 [英] How to read kmz file of google map in android
问题描述
我有KMZ文件,我想解析该KMZ文件,以便我可以读取尝试使用 KmlLayer
的文件的数据,但是没有得到任何帮助,这是我的代码
I have the KMZ file and I want to parse that KMZ file so that I can read the data of that file I tried to use KmlLayer
but didn't get any help from it here is my code
InputStream inputStream = new FileInputStream(path);
KmlLayer layer = new KmlLayer(mMap, inputStream, getApplicationContext());
layer.addLayerToMap();
但是在创建任何解决方案的KmlLayer对象时,我都遇到了解析异常.
but I got Parsing exception while I am creating the object of KmlLayer any solution.
推荐答案
因为 KMZ 压缩为 KML ,则应 ZipInputStream
而不是 FileInputStream
就像这样的 createLayerFromKmz()
方法:
Because KMZ is zipped KML you should unzip .kmz
file to .kml
before reading data or use ZipInputStream
instead of FileInputStream
like in this createLayerFromKmz()
method:
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;
}
您可以使用它,例如这样:
And you can use it e.g. this way:
@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();
}
}
NB! createLayerFromKmz()
仅适用于扁平" KMZ结构.
NB! createLayerFromKmz()
works only on "flat" KMZ structure.
这篇关于如何在Android中读取Google Map的kmz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!