How to parse KML file in Android

倾然丶 夕夏残阳落幕 提交于 2019-12-08 13:22:02

问题


I want to know a simple and easy way to parse KML file and store its data in an object so that I can access its data instantly here is my kml file


回答1:


You can use KmlContainer from Google Maps KML Importing Utility to access any property in a container:

...
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"));
    }
}
...

For exactly yours kml file for standard geometry you can use something like this:

@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();
    }
}

and get something like that:

but for <ExtendedData> you should use external library with KML parsing support like GeoTools or parse your KML file as XML.



来源:https://stackoverflow.com/questions/53555485/how-to-parse-kml-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!