如何从kml层android获取点列表

时间:2016-04-13 14:07:49

标签: android google-maps android-maps

我正在尝试在我的Android应用中获取我的kml图层的坐标,而我似乎无法找到该怎么做。

我有这个kml层:

KmlLayer layer = new KmlLayer(mMap, R.raw.allowedarea, getApplicationContext());

我正试图获得他的边界点的纬度和经度列表。

ArrayList<LatLnt> latlitArray = layer.soemthing();

可以找到任何东西,请大家帮忙。

2 个答案:

答案 0 :(得分:1)

这将递归访问容器内的每个地标几何体。我不知道获得的对象是否实际上是List和LatLng之外的任何其他类或集合的实例。

public void accessContainers(Iterable<KmlContainer> containers) {
    for(KmlContainer c : containers) {
        if(c.hasPlacemarks()) {
            for(KmlPlacemark p : c.getPlacemarks()) {
                KmlGeometry g = p.getGeometry();
                Object object = g.getGeometryObject();

                if(object instanceof LatLng) {
                    LatLng latlng = (LatLng)object;
                    //Do more stuff with the point
                }

                if(object instanceof List<?>) {
                    List<LatLng> list = (List<LatLng>)object;
                    //Do more stuff with the list of points
                }

                Log.d(TAG, g.getGeometryType() + ":" + object.toString());
            }
        }
        if(c.hasContainers()) {
            accessContainers(c.getContainers());
        }
    }
}

答案 1 :(得分:1)

尝试此解决方案

try {
        KmlLayer layer = new KmlLayer(googleMap, R.raw.zone, this);
        layer.addLayerToMap();

        Iterable<KmlContainer> containers = layer.getContainers();
        accessContainers(containers);
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }



public void accessContainers(Iterable<KmlContainer> containers) {
            for (KmlContainer container : containers) {
                if (container != null) {
                    if (container.hasContainers()) {
                        accessContainers(container.getContainers());
                    } else {
                        if (container.hasPlacemarks()) {
                            accessPlacemarks(container.getPlacemarks());
                        }
                    }
                }
            }
        }

    public void accessPlacemarks(Iterable<KmlPlacemark> placemarks) {
        for (KmlPlacemark placemark : placemarks) {
            if (placemark != null) {
                KmlGeometry geometry = placemark.getGeometry();
                if (geometry instanceof KmlPolygon) {
                    KmlPolygon polygon = (KmlPolygon) geometry;
                    mLatLngList.addAll(polygon.getOuterBoundaryCoordinates());
                }
            }
        }
    }