Retrieve the event of clicking on a MapView

1254
3
08-02-2017 09:09 AM
ManelKEDDAR
New Contributor III

i'm trying to make an application for adding a new feature .SO the user click on the map  to specify a new feature's location. i do this by listening to a click event on mymap view, which in turn will call a function for adding a new feature .but this doesn't work i think the problem is how i provide the listening event here's my code:

public MainWindow()
{
InitializeComponent();
MyMapView.Map = new Map();
MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
}

private void MyMapView_GeoViewTapped(object sender , Esri.ArcGISRuntime.UI.Controls.GeoViewInputEventArgs e)
{
var mapClickPoint = e.Location;
//Appel de la fonction qui fait l'ajout d'un nouveau feature
addNewFeature(mapClickPoint);
}

private async void addNewFeature(MapPoint locationPoint)
{
var featureTableUri = new System.Uri("https://frpargeosidport.corp.capgemini.com/arcgis/rest/services/POCSynopsis/FeatureServer/0");
var table = new ServiceFeatureTable(featureTableUri);

await table.LoadAsync();
var builder = new PolygonBuilder(SpatialReference.Create(2154));

var pointPoly = new Esri.ArcGISRuntime.Geometry.PointCollection(SpatialReference.Create(2154));
pointPoly.Add(locationPoint.X, locationPoint.Y);
pointPoly.Add(1500, 1400);
pointPoly.Add(1300, 1200);
pointPoly.Add(1300, 1200);
pointPoly.Add(locationPoint.X, locationPoint.Y);
var poly = new Esri.ArcGISRuntime.Geometry.Polygon(pointPoly);
var description = "description";
var attributes = new Dictionary<string, object>();
attributes.Add("SHAPE", poly);
if (table.LoadStatus == Esri.ArcGISRuntime.LoadStatus.Loaded)
{


var template = table.FeatureTemplates.FirstOrDefault() ?? table.FeatureTypes.FirstOrDefault(t => t.Templates.Any())?.Templates?.FirstOrDefault();
var feature = table.CreateFeature(template,poly);
await table.AddFeatureAsync(feature);

}
//await table.DeleteFeatureAsync(selectedFeatures);
// push this update (apply edits) to the feature service
IReadOnlyList<EditResult> editResults = await table.ApplyEditsAsync();
// check the results for errors
foreach (var r in editResults)
{
if (r.CompletedWithErrors)
{
Console.WriteLine("Edit to Object '" + r.ObjectId + "' failed: " + r.Error.Message);
}
}

var layer = new FeatureLayer(table);
MyMapView.Map.OperationalLayers.Add(layer);


}

This doesn't show me any result any one can help me please !

Thanks

0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor

`GeoViewTapped` is the correct event. `CreateFeature`, `AddFeatureAsync`, and `ApplyEditsAsync` are all the right methods. Are you getting any error message from `ApplyEditsAsync`? I'm curious why you don't add the layer to your map before creating and adding the feature.

You can try the following sample code. When layer is added to the map, the map will load the layer and the layer will load the table, so you won't need to explicitly call `LoadAsync`.

        public MainWindow()
        {
            InitializeComponent();

            MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
            MyMapView.Map = new Map(Basemap.CreateTopographic());
            MyMapView.Map.OperationalLayers.Add(new FeatureLayer(new Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Notes/FeatureServer/0")));
        }

        private async void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.UI.Controls.GeoViewInputEventArgs e)
        {
            try
            {
                var layer = MyMapView.Map.OperationalLayers.FirstOrDefault() as FeatureLayer;
                var table = layer.FeatureTable as ServiceFeatureTable;
                var template = table.FeatureTemplates.FirstOrDefault() ?? table.FeatureTypes.FirstOrDefault(t => t.Templates.Any()).Templates.FirstOrDefault();
                var feature = table.CreateFeature(template);
                feature.Geometry = e.Location;
                await table.AddFeatureAsync(feature);
                var edits = await table.ApplyEditsAsync();
                var editResult = edits.FirstOrDefault(r => r is FeatureEditResult && r.CompletedWithErrors || r.Error != null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name);
            }            
        }
ManelKEDDAR
New Contributor III

Thanks for your answer , is there any way how i can communicate with you except the forum ? 

0 Kudos
ManelKEDDAR
New Contributor III

Hello ,

i've tried your method and i used table.createFeature(IEnumerable,Geometry) but when i execute my code it says that the method is not supported : 

public MainWindow()
{
InitializeComponent();

MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
MyMapView.Map = new Map();
MyMapView.Map.OperationalLayers.Add(new FeatureLayer(new Uri("https://frpargeosidport.corp.capgemini.com/arcgis/rest/services/POCSynopsis/FeatureServer/0")));
}

private async void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.UI.Controls.GeoViewInputEventArgs e)
{
try
{
var layer = MyMapView.Map.OperationalLayers.FirstOrDefault() as FeatureLayer;
var table = layer.FeatureTable as Esri.ArcGISRuntime.Data.ServiceFeatureTable;
var builder = new PolygonBuilder(SpatialReference.Create(2154));
var pointPoly = new Esri.ArcGISRuntime.Geometry.PointCollection(SpatialReference.Create(2154));
pointPoly.Add(e.Location.X,e.Location.Y);
pointPoly.Add(100, 100);
pointPoly.Add(200, 100);
pointPoly.Add(500, 500);
pointPoly.Add(e.Location.X, e.Location.Y);
var poly = new Esri.ArcGISRuntime.Geometry.Polygon(pointPoly);
var attributes = new Dictionary<string, object>();
attributes.Add("SHAPE", poly);
var template = table.FeatureTemplates.FirstOrDefault() ?? table.FeatureTypes.FirstOrDefault(t => t.Templates.Any()).Templates.FirstOrDefault();
var feature = table.CreateFeature(attributes,poly);
feature.Geometry = e.Location;
await table.AddFeatureAsync(feature);
var edits = await table.ApplyEditsAsync();
var editResult = edits.FirstOrDefault(r => r is FeatureEditResult && r.CompletedWithErrors || r.Error != null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name);
}
}


// Map initialization logic is contained in MapViewModel.cs
}

i have a feature class with a geometry field of polygon type 

0 Kudos