【问题标题】:how to get list of points from kml layer android如何从kml层android获取点列表
【发布时间】:2016-04-13 14:07:49
【问题描述】:

我正在尝试在我的 android 应用程序中获取我的 kml 层的坐标,但我似乎无法找到如何去做。

我有这个 kml 层:

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

我正在尝试获取他的边界点的纬度和经度列表。

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

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

【问题讨论】:

标签: android google-maps android-maps


【解决方案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());
                }
            }
        }
    }

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
【解决方案2】:

这将递归访问容器内的每个地标几何图形。我不知道获得的对象是否实际上可以是除 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());
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2023-03-29
    相关资源
    最近更新 更多