首页 > 解决方案 > 在 ArcGIS 图形图层中移动和/或移除标记

问题描述

我正在使用 ArcGIS 100.6 iOS SDK 并使用图形叠加层上的标记填充地图,其位置存储在我的应用程序的所有用户通用的数据库中。每个标记都存储在唯一的记录中,每个记录都包含标记的纬度和经度。当应用程序启动时,它会读取数据库中所有标记的位置并将每个标记添加到图形叠加中,如下所示:

let areaMarker = AGSPictureMarkerSymbol(image: UIImage(named: "CustomMarker")!)
let areaMarkerLocation = AGSPointMakeWGS84(y ?? 0.0, x ?? 0.0)
let markerIcon = AGSGraphic(geometry: areaMarkerLocation, symbol: areaMarker, attributes: >["marker": markerKey])
self.overlay.graphics.add(markerIcon)

如上所示,每个标记都分配有一个属性“marker:markerKey”,该属性是存储标记位置信息的唯一数据库记录号(键),并用作标记ID。

将初始标记添加到叠加层后,应用程序“侦听”数据库中的以下事件:

当标记被移动或删除时,会通知数据库侦听器并传递已移动(或删除)的标记的记录号(键)。如果标记被移动,则记录将包含新的纬度和经度信息。

我已经尝试阅读图形覆盖并确定它是包含在 NSMutable 数组中的集合。我可以读取所有属性如下:

let graphicsCollection = self.overlay.graphics.mutableArrayValue(forKey: "attributes")
print(graphicsCollection)

结果是:

(
        {
        marker = "-KlRW2_rba1zBrDPpxSl";
    },
{
        marker = "-Lu915xF3zQp4dIYnsP_";
    }
)

我可以对“几何”做同样的事情并获得 AGSPoints 数组:

let graphicsCollection = self.overlay.graphics.mutableArrayValue(forKey: "geometry")
print(graphicsCollection)

结果是:

(
    "AGSPoint: (-117.826127, 44.781139), sr: 4326",
    "AGSPoint: (-112.056906, 33.629829), sr: 4326"
)

我无法确定如何获取属性数组的“索引”(例如,上面的标记“-KlRW2_rba1zBrDPpxSl”的索引应该为 [0]),因此我可以使用该“索引”访问相应的 AGSPoint 并更新纬度和经度或删除标记。

在此先感谢您的帮助。

标签: iosswiftnsmutablearrayarcgis-runtime

解决方案


如果您想移动标记(即一个AGSGraphic),您需要获取它AGSGraphic本身并修改geometry属性。我认为通过在你的mutableArrayValue()电话中跳到“几何”,你有点在踢自己。

我会这样解决它:

let searchMarker = "-KlRW2_rba1zBrDPpxSl"
let newLocation = AGSPointMakeWGS84(40.7128, -74.0060) // NYC
if let graphic = (overlay.graphics as? [AGSGraphic])?.first(where: { 
    ($0.attributes["marker"] as? String) == searchMarker
}) {
    // Move the graphic
    graphic.geometry = newLocation
    // Or remove the graphic
    overlay.graphics.remove(graphic)
}

推荐阅读