Update a feature's location in an online feature service.
Use case
Sometimes users may want to edit features in an online feature service by moving them.
How to use the sample
Tap a feature to select it. Tap again to set the updated location for that feature. An alert will be shown confirming success or failure.
How it works
- Create a
ServiceFeatureTable
object from a URL. - Create a
FeatureLayer
object from theServiceFeatureTable
. - Select a feature from the feature layer, using
FeatureLayer.SelectFeatures
. - Load the selected feature.
- Change the selected feature's location using
Feature.Geometry = geometry
. - After the change, update the table on the server using
ApplyEditsAsync
.
Relevant API
- Feature
- FeatureLayer
- ServiceFeatureTable
Tags
editing, feature layer, feature table, moving, service, updating
Sample Code
UpdateGeometries.cs
// Copyright 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using ArcGISRuntime;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System;
using System.Linq;
using UIKit;
namespace ArcGISRuntimeXamarin.Samples.UpdateGeometries
{
[Register("UpdateGeometries")]
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
name: "Update geometries (feature service)",
category: "Data",
description: "Update a feature's location in an online feature service.",
instructions: "Tap a feature to select it. Tap again to set the updated location for that feature. An alert will be shown confirming success or failure.",
tags: new[] { "editing", "feature layer", "feature table", "moving", "service", "updating" })]
public class UpdateGeometries : UIViewController
{
// Hold references to UI controls.
private MapView _myMapView;
// URL to the feature service.
private const string FeatureServiceUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0";
// Hold a reference to the feature layer.
private FeatureLayer _damageLayer;
// Hold a reference to the selected feature.
private ArcGISFeature _selectedFeature;
public UpdateGeometries()
{
Title = "Update geometries (feature service)";
}
private void Initialize()
{
// Create the map with streets basemap.
_myMapView.Map = new Map(BasemapStyle.ArcGISStreets);
// Create the feature table, referring to the Damage Assessment feature service.
ServiceFeatureTable damageTable = new ServiceFeatureTable(new Uri(FeatureServiceUrl));
// Create a feature layer to visualize the features in the table.
_damageLayer = new FeatureLayer(damageTable);
// Add the layer to the map.
_myMapView.Map.OperationalLayers.Add(_damageLayer);
// Zoom to the United States.
_myMapView.SetViewpointCenterAsync(new MapPoint(-10800000, 4500000, SpatialReferences.WebMercator), 3e7);
}
private void MapView_Tapped(object sender, GeoViewInputEventArgs e)
{
// Skip if the sample isn't ready.
if (_damageLayer == null) return;
// Select the feature if none selected, move the feature otherwise.
if (_selectedFeature == null)
{
// Select the feature.
TrySelectFeature(e);
}
else
{
// Move the feature.
MoveSelectedFeature(e);
}
}
private async void MoveSelectedFeature(GeoViewInputEventArgs tapEventDetails)
{
try
{
// Get the MapPoint from the EventArgs for the tap.
MapPoint destinationPoint = tapEventDetails.Location;
// Normalize the point - needed when the tapped location is over the international date line.
destinationPoint = (MapPoint) GeometryEngine.NormalizeCentralMeridian(destinationPoint);
// Load the feature.
await _selectedFeature.LoadAsync();
// Update the geometry of the selected feature.
_selectedFeature.Geometry = destinationPoint;
// Apply the edit to the feature table.
await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
// Push the update to the service.
ServiceFeatureTable serviceTable = (ServiceFeatureTable) _selectedFeature.FeatureTable;
await serviceTable.ApplyEditsAsync();
ShowMessage("Success!", $"Moved feature {_selectedFeature.Attributes["objectid"]}");
}
catch (Exception ex)
{
ShowMessage("Error when moving feature", ex.ToString());
}
finally
{
// Reset the selection.
_damageLayer.ClearSelection();
_selectedFeature = null;
}
}
private async void TrySelectFeature(GeoViewInputEventArgs tapEventDetails)
{
try
{
// Perform an identify to determine if a user tapped on a feature.
IdentifyLayerResult identifyResult = await _myMapView.IdentifyLayerAsync(_damageLayer, tapEventDetails.Position, 10, false);
// Do nothing if there are no results.
if (!identifyResult.GeoElements.Any())
{
return;
}
// Get the tapped feature.
_selectedFeature = (ArcGISFeature) identifyResult.GeoElements.First();
// Select the feature.
_damageLayer.SelectFeature(_selectedFeature);
}
catch (Exception ex)
{
ShowMessage("Problem selecting feature", ex.ToString());
}
}
private void ShowMessage(string title, string message)
{
// Create the alert controller.
UIAlertController alertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
// Show the alert.
PresentViewController(alertController, true, null);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Initialize();
}
public override void LoadView()
{
// Create the views.
View = new UIView() { BackgroundColor = ApplicationTheme.BackgroundColor };
_myMapView = new MapView();
_myMapView.TranslatesAutoresizingMaskIntoConstraints = false;
UILabel helpLabel = new UILabel
{
Text = "Tap to select a feature. Tap again to move it.",
AdjustsFontSizeToFitWidth = true,
TextAlignment = UITextAlignment.Center,
BackgroundColor = UIColor.FromWhiteAlpha(0, .6f),
TextColor = UIColor.White,
Lines = 1,
TranslatesAutoresizingMaskIntoConstraints = false
};
// Add the views.
View.AddSubviews(_myMapView, helpLabel);
// Lay out the views.
NSLayoutConstraint.ActivateConstraints(new[]
{
_myMapView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
_myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
_myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
_myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
helpLabel.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
helpLabel.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
helpLabel.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
helpLabel.HeightAnchor.ConstraintEqualTo(40)
});
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
// Listen for user taps on the map - on tap, a feature will be selected.
_myMapView.GeoViewTapped += MapView_Tapped;
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
// Unsubscribe from events, per best practice.
_myMapView.GeoViewTapped -= MapView_Tapped;
}
}
}