Adding a point feature at a device's current location

231
7
10-10-2018 08:29 AM
GiatriLalla
New Contributor III

Good Day,

I have been using the code below to zoom to the device's location and add a point feature based on the device's current location by clicking any where on the screen. The feature is being added to the service table however there is no/null geometries for each point therefore it is not showing up on the map. How can I fix this?

import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Controls.Material 2.1
import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.0
import ArcGIS.AppFramework 1.0
import ArcGIS.AppFramework.Controls 1.0
import Esri.ArcGISRuntime 100.2
import QtPositioning 5.3
import QtSensors 5.3
MapView{
    id: mapView
    Map{
        id: map
        initUrl: "http://melbournedev.maps.arcgis.com/home/webmap/viewer.html?webmap=24ba3f20e7424c67989e64ec740384f8"
        FeatureLayer {
            id: featureLayer
            selectionColor: "cyan"
            selectionWidth: 3
            // declare as child of feature layer, as featureTable is the default property
            ServiceFeatureTable {
                id: featureTable
                url: "https://services1.arcgis.com/NUSHso3rgWERZOFF/arcgis/rest/services/TestPoint1/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();
                    }
                }
            }
        }
    }
    locationDisplay {
        positionSource: positionSource
        compass: compass
    }
    PositionSource {
        id: positionSource
        active: true
        property bool isInitial: true
        onPositionChanged: {
            if(map.loadStatus === Enums.LoadStatusLoaded && isInitial) {
                isInitial = false;
                zoomToCurrentLocation();
            }
        }
    }
    Compass {
        id: compass
    }
    function zoomToCurrentLocation(){
        positionSource.update ();
        var currentPositionPoint = ArcGISRuntimeEnvironment.createObject ("Point", {x: positionSource.position.coordinate.longitude, y: positionSource.position.coordinate.latitude, spatialReference: SpatialReference.createWgs84()});
        var centerPoint = GeometryEngine.project(currentPositionPoint, mapView.spatialReference);
        var viewPointCenter = ArcGISRuntimeEnvironment.createObject("ViewpointCenter",{center: centerPoint, targetScale: 10000});
        mapView.setViewpointWithAnimationCurve(viewPointCenter, 2.0,  Enums.AnimationCurveEaseInOutCubic);
    }
    //! [AddFeaturesFeatureService new feature at mouse click]
     onMouseClicked : {  // mouseClicked came from the MapView
        // create attributes json for the new feature
         var featureAttributes = {"Name" : "Test"};
        // create a new feature using the mouse's map point
        var feature = featureTable.createFeatureWithAttributes(featureAttributes, mapView.locationDisplay.mapPoint);
         // add the new feature to the feature table
        featureTable.addFeature(feature);
    }
    }

					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
0 Kudos
7 Replies
ErwinSoekianto
Esri Regular Contributor

Giatri, 

Can you check the "addFeatureStatus" property? As stated in this doc when calling "addFeature()" function. FeatureTable QML Type | ArcGIS for Developers 

It sounds like this type of question is best handled by Esri Technical Support‌ as it might require further troubleshooting. 

Thanks,

Erwin

0 Kudos
GiatriLalla
New Contributor III

Correct me if I am wrong, I believe that the problem lies in the mapView.locationDisplay.mapPoint;

 

  var feature = featureTable.createFeatureWithAttributes(featureAttributes, mapView.locationDisplay.mapPoint);

I came to this conclusion because if I replace
  var feature = featureTable.createFeatureWithAttributes(featureAttributes, mouse.mapPoint) I can add random features and it works fine.

What are your thoughts?
0 Kudos
ErwinSoekianto
Esri Regular Contributor

It could be. The addFeatureStatus would have made sure what's wrong. 

So mapView.locationDisplay.mapPoint return null or undefined? It sounds like you are already progressing towards the resolution.

This documentation, LocationDisplay QML Type | ArcGIS for Developers , explains that LocationDisplay does not have "mapPoint" property, maybe you could try "mapLocation" property instead?

GiatriLalla
New Contributor III

I will try that and let you know. Thank you.

0 Kudos
GiatriLalla
New Contributor III

Hi,

I tried the "mapLocation" property

(var point = mapView.locationDisplay.mapLocation)

 and I am getting 0,0 now. In the greater scheme of things, atleast it's not null however still can't figure out why I can't get the correct geometry. Also, how do I check the addFeatureStatus?

0 Kudos
ErwinSoekianto
Esri Regular Contributor

The reason why it is returning 0,0 could be in the description of Location Display documentation, LocationDisplay QML Type | ArcGIS for Developers  

I still think that this type of question is best handled via Esri Technical Support‌, due to the nature of it and it requires more troubleshooting and back/forth. 

Thank you,

Erwin

GiatriLalla
New Contributor III

Will check that out.

Yes I have put forward the question, however no one from the team replied.

0 Kudos