如何在 Android 中解析 KML 文件
How to parse KML file in Android
我想知道一种简单易行的方法来解析 KML 文件并将其数据存储在对象中,以便我可以立即访问其数据,这里是 my kml file
您可以使用 KmlContainer
from Google Maps KML Importing Utility 访问容器中的任何 属性:
...
KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());
Iterable containers = layer.getContainers();
for (KmlContainer container : containers ) {
if (container.hasProperty("property_name")) {
// process property
Log.d(TAG, "" + container.getProperty("property_name"));
}
}
...
对于你的 kml
标准几何文件,你可以使用这样的东西:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(17.425868, 78.459761), 16));
// change next line for your kml source
InputStream kmlInputStream = getResources().openRawResource(R.raw.data);
try {
KmlLayer kmlLayer = new KmlLayer(mGoogleMap, kmlInputStream, getApplicationContext());
kmlLayer.addLayerToMap();
ArrayList<LatLng> pathPoints = new ArrayList();
if (kmlLayer != null && kmlLayer.getContainers() != null) {
for (KmlContainer container : kmlLayer.getContainers()) {
if (container.hasPlacemarks()) {
for (KmlPlacemark placemark : container.getPlacemarks()) {
Geometry geometry = placemark.getGeometry();
if (geometry.getGeometryType().equals("Point")) {
KmlPoint point = (KmlPoint) placemark.getGeometry();
LatLng latLng = new LatLng(point.getGeometryObject().latitude, point.getGeometryObject().longitude);
pathPoints.add(latLng);
} else if (geometry.getGeometryType().equals("LineString")) {
KmlLineString kmlLineString = (KmlLineString) geometry;
ArrayList<LatLng> coords = kmlLineString.getGeometryObject();
for (LatLng latLng : coords) {
pathPoints.add(latLng);
}
}
}
}
}
for (LatLng latLng : pathPoints) {
mGoogleMap.addMarker(new MarkerOptions().position(latLng));
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
并得到类似的东西:
但对于 <ExtendedData>
,您应该使用支持 KML 解析的外部库,例如 GeoTools,或者将您的 KML 文件解析为 XML。
我想知道一种简单易行的方法来解析 KML 文件并将其数据存储在对象中,以便我可以立即访问其数据,这里是 my kml file
您可以使用 KmlContainer
from Google Maps KML Importing Utility 访问容器中的任何 属性:
...
KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());
Iterable containers = layer.getContainers();
for (KmlContainer container : containers ) {
if (container.hasProperty("property_name")) {
// process property
Log.d(TAG, "" + container.getProperty("property_name"));
}
}
...
对于你的 kml
标准几何文件,你可以使用这样的东西:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(17.425868, 78.459761), 16));
// change next line for your kml source
InputStream kmlInputStream = getResources().openRawResource(R.raw.data);
try {
KmlLayer kmlLayer = new KmlLayer(mGoogleMap, kmlInputStream, getApplicationContext());
kmlLayer.addLayerToMap();
ArrayList<LatLng> pathPoints = new ArrayList();
if (kmlLayer != null && kmlLayer.getContainers() != null) {
for (KmlContainer container : kmlLayer.getContainers()) {
if (container.hasPlacemarks()) {
for (KmlPlacemark placemark : container.getPlacemarks()) {
Geometry geometry = placemark.getGeometry();
if (geometry.getGeometryType().equals("Point")) {
KmlPoint point = (KmlPoint) placemark.getGeometry();
LatLng latLng = new LatLng(point.getGeometryObject().latitude, point.getGeometryObject().longitude);
pathPoints.add(latLng);
} else if (geometry.getGeometryType().equals("LineString")) {
KmlLineString kmlLineString = (KmlLineString) geometry;
ArrayList<LatLng> coords = kmlLineString.getGeometryObject();
for (LatLng latLng : coords) {
pathPoints.add(latLng);
}
}
}
}
}
for (LatLng latLng : pathPoints) {
mGoogleMap.addMarker(new MarkerOptions().position(latLng));
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
并得到类似的东西:
但对于 <ExtendedData>
,您应该使用支持 KML 解析的外部库,例如 GeoTools,或者将您的 KML 文件解析为 XML。