【问题标题】:How to properly add/delete feature to Feature List - mapbox如何正确添加/删除功能到功能列表 - mapbox
【发布时间】:2020-07-16 18:27:47
【问题描述】:

我有两种方法可以删除/添加标记到我的应用程序。当数据库发生变化时,标记将被添加或删除。在我将属性“POPUP”添加到我的功能列表之前,一切正常。 现在,当我有地理定位和 POPUP 属性时 - 从功能列表中删除功能不起作用。我如何通过传递 Geopoint 和 POPUP 属性来删除功能。

我需要保存要素 Geopoint 和字符串值以及何时需要 - 删除它们。保存工作正常。

    private void  delMarkers(double lng, double lat, Style style, String popup_data){
    //THIS LINE BELOW IS NOT WORKING
    symbolLayerIconFeatureList.remove(Feature.fromGeometry(Point.fromLngLat(lng,lat)));
    /////////////////////
    GeoJsonSource geoJsonSourceSymbol = style.getSourceAs(SOURCE_ID);
    if (geoJsonSourceSymbol != null) {
        geoJsonSourceSymbol.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));
    }
}
private void addMarkers(double lng, double lat, Style style, String popup_data){
    feature = Feature.fromGeometry(Point.fromLngLat(lng,lat));
    feature.addStringProperty(POPUP, popup_data);
    symbolLayerIconFeatureList.add(feature);

    GeoJsonSource geoJsonSourceSymbol = style.getSourceAs(SOURCE_ID);
    if (geoJsonSourceSymbol != null) {
        geoJsonSourceSymbol.setGeoJson(FeatureCollection.fromFeatures(symbolLayerIconFeatureList));
    }
}

【问题讨论】:

    标签: android mapbox


    【解决方案1】:

    认为symbolLayerIconFeatureList.remove(Feature.fromGeometry(Point.fromLngLat(lng,lat))); 行给你带来了问题。你不应该这样做Feature.fromGeometry(Point.fromLngLat(lng,lat)),因为这会创建一个新的Feature

    根据您拥有的Features 数量,它可能不是很高效,但您可以解析您的Feature 列表,将每个Feature 的纬度/经度与您的经度/纬度进行比较通过delMarkers() 方法。确定应该删除哪个Feature 后,将其传递给remove() 方法。

    【讨论】:

    • 我会尝试比较纬度/经度,但我害怕性能。线符号LayerIconFeatureList.remove(Feature.fromGeometry(Point.fromLngLat(lng,lat)));当我不添加 POPUP 属性时工作正常。我认为也许有一种方法可以通过解析两个参数来从列表中删除该功能
    • 你知道如何通过坐标制作一个好的foreach循环吗?我试过这样,但它使应用程序崩溃(功能功能:symbolLayerIconFeatureList){ if(feature.getStringProperty(POPUP).equals(popup_data)){symbolLayerIconFeatureList.remove(feature); } 其他{ } }
    • 好的,我找到了一种使用迭代器的方法,我将使用它并用更大的数字检查性能。迭代器 i = symbolLayerIconFeatureList.iterator(); while (i.hasNext()) { 特征 s = i.next(); // 必须在调用 i.remove() 之前调用 if(s.geometry().equals(geometry1)) { i.remove(); } }
    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多