Add a feature

716
5
11-01-2018 11:22 AM
GiatriLalla
New Contributor III

Hi,

I am trying to add a feature as soon as an app is opened. I tried the code below but it is not working. The location is turning on however, the feature is not showing up. How can I fix this?

       Component.onCompleted: {
            //Qt.openUrlExternally("%1/home/webmap/viewer.html?webmap=%2".arg(app.portalUrl).arg(portalItem.id))
            if (!mapView.locationDisplay.started) {
                mapView.locationDisplay.start()
                mapView.locationDisplay.autoPanMode = Enums.LocationDisplayAutoPanModeRecenter
                    // create attributes json for the new feature
                    var featureAttributes = {"xxxx" : "xxxx"};
                    // create a new feature using the mouse's map point
                    var point = mapView.locationDisplay.mapLocation
                    var feature = featureTable.createFeatureWithAttributes(featureAttributes, point);
                    // add the new feature to the feature table
                    featureTable.addFeature(feature);
            } else {
                mapView.locationDisplay.stop()
            }
        }
Tags (2)
0 Kudos
5 Replies
ErwinSoekianto
Esri Regular Contributor

Giatri, 

What is the error message you are getting? Does that block of code work if you were to run it with a click of a button for example? 

This should work, this type of question requires further troubleshooting, I would recommend calling Esri Technical Support‌ to get help. 

Erwin.

GiatriLalla
New Contributor III

Yes, the code runs if I had a Button to click. There is no error message. The point feature just don't show up while the location turns on like normal. That's why I can't figure out how to fix this.

0 Kudos
ErwinSoekianto
Esri Regular Contributor

My guess is it might be a timing issue between the "Completed.onCompleted" being triggered and the location display is ready. 

Can you check if we have the point before adding the feature to featureTable?

0 Kudos
GiatriLalla
New Contributor III

No feature is being created, nothing is happening.

0 Kudos
nakulmanocha
Esri Regular Contributor

Hi Giatri,

Please make sure the feature layer has been loaded (if adding it to the map) or initialized (if not the part of the view).

For e.g. here I am adding the feature as soon as the featurelayer loads. Also Make your point is not undefined. I hope this helps.

Nakul

 FeatureLayer {
                id: featureLayer


               

                // declare as child of feature layer, as featureTable is the default property
                ServiceFeatureTable {
                    id: featureTable
                    url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0"

                    // make sure edits are successfully applied to the service
                    onApplyEditsStatusChanged: {
                        if (applyEditsStatus === Enums.TaskStatusCompleted) {
                            console.log("successfully added feature");
                        }
                    }

                    // signal handler for the asynchronous addFeature method
                    onAddFeatureStatusChanged: {
                        if (addFeatureStatus === Enums.TaskStatusCompleted) {
                            // apply the edits to the service
                            featureTable.applyEdits();

                        }
                    }
                }
                onLoadStatusChanged: {
                   if(loadStatus === Enums.LoadStatusLoaded){
                        // var point = ArcGISRuntimeEnvironment.createObject("Point", {x: -11975850.0012, y: 4274812.4997999966, spatialReference: SpatialReference.createWebMercator()});
                       var point = mapView.locationDisplay.mapLocation
                       var featureAttributes = {"typdamage" : "Minor", "primcause" : "Earthquake"};
                       // create a new feature using the mouse's map point
                       var feature = featureTable.createFeatureWithAttributes(featureAttributes, point);

                       // add the new feature to the feature table
                       featureTable.addFeature(feature);
                   }
                }
            }
        }
0 Kudos