如何读取 android 中 google 地图的 kmz 文件
How to read kmz file of google map in android
我有 KMZ 文件,我想解析该 KMZ 文件,以便我可以读取我尝试使用的那个文件的数据 KmlLayer
,但没有得到任何帮助这是我的代码
InputStream inputStream = new FileInputStream(path);
KmlLayer layer = new KmlLayer(mMap, inputStream, getApplicationContext());
layer.addLayerToMap();
但是我在创建 KmlLayer 任何解决方案的对象时遇到解析异常。
因为KMZ is zipped KML you should unzip .kmz
file to .kml
before reading data or use ZipInputStream
instead of FileInputStream
喜欢这个createLayerFromKmz()
方法:
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()
仅适用于 "flat" KMZ 结构。
我有 KMZ 文件,我想解析该 KMZ 文件,以便我可以读取我尝试使用的那个文件的数据 KmlLayer
,但没有得到任何帮助这是我的代码
InputStream inputStream = new FileInputStream(path);
KmlLayer layer = new KmlLayer(mMap, inputStream, getApplicationContext());
layer.addLayerToMap();
但是我在创建 KmlLayer 任何解决方案的对象时遇到解析异常。
因为KMZ is zipped KML you should unzip .kmz
file to .kml
before reading data or use ZipInputStream
instead of FileInputStream
喜欢这个createLayerFromKmz()
方法:
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()
仅适用于 "flat" KMZ 结构。