Add Point on AGO layer with no Map

463
4
12-17-2018 12:47 PM
by Anonymous User
Not applicable

I need to add a point with XY location and couple of attributes to an AGO layer. The view controller from where I need to add a point has no map component. Is there a way to make this work?

0 Kudos
4 Replies
Nicholas-Furness
Esri Regular Contributor

Yes. You do not need a map to edit (or even read/query) data.

Simply create an AGSServiceFeatureTable that points at your ArcGIS Online layer.

You can then follow the pattern described in the Add Features sample. There's no need to create an AGSFeatureLayer from the table to add to a map.

Let me know how that works.

0 Kudos
by Anonymous User
Not applicable

Thats what I thought earlier and I was trying to do same as github sample

let point = AGSPoint(x:long,y:lat,spatialReference: AGSSpatialReference.wgs84())

let attr = ["Name":"a","Type":"b"]

let feature = self.featureTable.createFeature(attributes: attr, geometry: point)

//add the feature to the feature table

self.featureTable.add(feature) { [weak self] (error: Error?) -> Void in

                    if let error = error {

                        print("Error while adding feature :: \(error)")

                    }

                    else {

                        //applied edits on success

                        self?.applyEdits()

                    }

}

and I am getting an error in 

Error Domain=com.esri.arcgis.runtime.error Code=1 "Null pointer" UserInfo={NSLocalizedFailureReason=feature cannot be null.

feature is returning as null from createFeature function.

0 Kudos
Nicholas-Furness
Esri Regular Contributor

Ah. Most likely featureTable is not loaded. Try something like this:

featureTable.load { [weak self] error in
    guard error == nil else {
        print("Error loading feature table! \(error!.localizedDescription)")
        return
    }
    
    guard let self = self else { return }
    
    let geom = AGSPointMakeWGS84(40.7128, -74.0060)
    let attr = ["Name":"a","Type":"b"]

    let feature = self.featureTable.createFeature(attributes: attr, geometry: geom)
    
    self.featureTable.add(feature) { error in
        guard error == nil else {
            print("Error adding the feature! \(error!.localizedDescription)")
            return
        }
        
        print("Feature added OK")
    }
}

Depending on your app, you could load the layer once up front and enable the UI once it's loaded, or you could follow this pattern each time (if an AGSLoadable object is already loaded, calls to load return immediately).

by Anonymous User
Not applicable

worked perfect !

Thanks

0 Kudos