如何在Android中解析KML文件 [英] How to parse KML file in Android

查看:714
本文介绍了如何在Android中解析KML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一种解析KML文件并将其数据存储在对象中的简单方法,这样我就可以在此处

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

推荐答案

您可以使用 Google Maps KML导入实用程序以访问容器中的任何属性:

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

对于标准几何图形的kml文件,您可以使用类似以下内容的文件:

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

得到类似的东西:

但对于<ExtendedData>,您应该使用具有KML解析支持的外部库,例如 GeoTools 或解析您的KML文件为XML.

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

这篇关于如何在Android中解析KML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆