Select to view content in your preferred language

How can i make an AGSGraphic not be selectable

417
1
06-07-2023 11:44 PM
VinceCarloSantos
New Contributor

How can i make an AGSGraphic not be selectable on geoView didTapAtScreenPoint?

I have graphics but there are some that i dont want the user to be able to select.


1 Reply
Ting
by Esri Contributor
Esri Contributor

There isn't a property on a geo-element to disable it from being selected. One idea would be using the attributes dictionary to set additional attributes on it, so you can use them to run custom logics.

Below is a code example of using attributes on a graphic. You can add custom logic to the identify method to decide if a graphic is selectable based on its attributes (Line 49).

 

import UIKit
import ArcGIS

class ViewController: UIViewController {
    @IBOutlet var mapView: AGSMapView! {
        didSet {
            mapView.map = AGSMap(basemapStyle: .arcGISTopographic)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 56.062,
                    longitude: -2.712,
                    scale: 1e5
                )
            )
            mapView.graphicsOverlays.add(graphicsOverlay)
            mapView.touchDelegate = self
        }
    }

    let graphicsOverlay: AGSGraphicsOverlay = AGSGraphicsOverlay()
    
    func addBuoyPoints(to graphicsOverlay: AGSGraphicsOverlay) {
        let buoy1Loc = AGSPoint(x: -2.712, y: 56.062, spatialReference: .wgs84())
        let buoy2Loc = AGSPoint(x: -2.690, y: 56.064, spatialReference: .wgs84())
        let buoy3Loc = AGSPoint(x: -2.669, y: 56.064, spatialReference: .wgs84())
        let buoy4Loc = AGSPoint(x: -2.639, y: 56.061, spatialReference: .wgs84())
        
        let buoyMarker = AGSSimpleMarkerSymbol(style: .circle, color: .red, size: 10)
        
        let buoyGraphic1 = AGSGraphic(geometry: buoy1Loc, symbol: buoyMarker, attributes: ["isSelectabale": false])
        let buoyGraphic2 = AGSGraphic(geometry: buoy2Loc, symbol: buoyMarker, attributes: ["isSelectabale": true])
        let buoyGraphic3 = AGSGraphic(geometry: buoy3Loc, symbol: buoyMarker, attributes: ["isSelectabale": false])
        let buoyGraphic4 = AGSGraphic(geometry: buoy4Loc, symbol: buoyMarker, attributes: ["isSelectabale": true])
        
        graphicsOverlay.graphics.addObjects(from: [buoyGraphic1, buoyGraphic2, buoyGraphic3, buoyGraphic4])
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addBuoyPoints(to: graphicsOverlay)
    }
}

extension ViewController: AGSGeoViewTouchDelegate {
    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        mapView.identify(graphicsOverlay, screenPoint: screenPoint, tolerance: 5, returnPopupsOnly: false) { [weak self] result in
            guard let self = self else { return }
            if result.graphics.isEmpty { graphicsOverlay.clearSelection(); return }
            let selectableGraphics = result.graphics.filter { $0.attributes["isSelectabale"] as? Bool ?? false }
            graphicsOverlay.selectGraphics(selectableGraphics)
        }
    }
}

 

Attributes dictionary can also be used for storing a variety of values. See another example of camera angle here.

0 Kudos