Select to view content in your preferred language

Line snapping is not working in my code

129
2
a week ago
Labels (2)
NagaMaheshBabupolana
New Contributor

Here i have written code for enabling snapping point and line for the existing line.But its not working in my code.Its working for point without any issue. can u help me out in adding new functions or features added to the code.
webmap-->Group Layer-->Feature Layers

on Initialize i am calling the EnableSnapping();

private async void EnableSnapping()
{
// Ensure all feature layers within the map are loaded
foreach (var layer in mapView.Map.OperationalLayers)
{
await LoadLayerRecursively(layer);
}

// Synchronize the snap source collection with the map's operational layers.
_geometryEditor.SnapSettings.SyncSourceSettings();

// Enable snapping on the geometry layer.
_geometryEditor.SnapSettings.IsEnabled = true;

// Optionally, configure snap tolerance and other settings
_geometryEditor.SnapSettings.Tolerance = 10.0; // Adjust tolerance as needed

// Create a list of snap source settings with snapping disabled initially
List<SnapSourceSettingsVM> snapSourceSettingsVMs = new List<SnapSourceSettingsVM>();

// Iterate through the feature layers to set snapping for points and polylines
foreach (var snapSourceSetting in _geometryEditor.SnapSettings.SourceSettings)
{
if (snapSourceSetting.Source is FeatureLayer featureLayer)
{
var snapSourceSettingsVM = new SnapSourceSettingsVM(snapSourceSetting) { IsEnabled = false };
snapSourceSettingsVMs.Add(snapSourceSettingsVM);
}
}

// Populate lists of snap source settings for point and polyline layers.
var pointSnapSettings = snapSourceSettingsVMs.Where(snapSourceSettingVM => snapSourceSettingVM.GeometryType == GeometryType.Point).ToList();
var polylineSnapSettings = snapSourceSettingsVMs.Where(snapSourceSettingVM => snapSourceSettingVM.GeometryType == GeometryType.Polyline).ToList();

PointSnapSettingsList.ItemsSource = pointSnapSettings;
PolylineSnapSettingsList.ItemsSource = polylineSnapSettings;

// Debug: Print counts of point and polyline snap settings
Debug.WriteLine($"Point Snap Settings Count: {pointSnapSettings.Count}");
Debug.WriteLine($"Polyline Snap Settings Count: {polylineSnapSettings.Count}");
}

private async Task LoadLayerRecursively(Layer layer)
{
if (layer is GroupLayer groupLayer)
{
foreach (var subLayer in groupLayer.Layers)
{
await LoadLayerRecursively(subLayer);
}
}
else if (layer is FeatureLayer featureLayer)
{
if (featureLayer.LoadStatus != Esri.ArcGISRuntime.LoadStatus.Loaded)
{
await featureLayer.LoadAsync();
}
}
}

private void EnableAllPolylineSnapSourceButton_Click(object sender, EventArgs e)
{
foreach (var item in PolylineSnapSettingsList.ItemsSource)
{
if (item is SnapSourceSettingsVM snapSourceSettingsVM)
{
snapSourceSettingsVM.IsEnabled = true;
}
}
}

private void EnableAllPointSnapSourceButton_Click(object sender, EventArgs e)
{
foreach (var item in PointSnapSettingsList.ItemsSource)
{
if (item is SnapSourceSettingsVM snapSourceSettingsVM)
{
snapSourceSettingsVM.IsEnabled = true;
}
}
}

public class SnapSourceSettingsVM : INotifyPropertyChanged
{
public SnapSourceSettings SnapSourceSettings { get; set; }

public SnapSourceSettingsVM(SnapSourceSettings snapSourceSettings)
{
SnapSourceSettings = snapSourceSettings;

if (snapSourceSettings.Source is FeatureLayer featureLayer && featureLayer.FeatureTable != null)
{
Name = featureLayer.Name;
GeometryType = featureLayer.FeatureTable.GeometryType;
}

IsEnabled = snapSourceSettings.IsEnabled;
}

private string _name;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged();
}
}

private bool _isEnabled;
public bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
SnapSourceSettings.IsEnabled = value;
OnPropertyChanged();
}
}

public GeometryType GeometryType { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}



0 Kudos
2 Replies
williambohrmann3
Esri Contributor

Hi @NagaMaheshBabupolana 
I tested the public code sample SnapGeometryEdits and line snapping is functioning correctly. Can you try modifying your code to follow this workflow? If you are still running into this bug, I would be happy to debug a simple reproducer if you can upload it to GitHub. Reproducer steps as well would be very helpful.

0 Kudos
williambohrmann3
Esri Contributor

I noticed that in the provided code snippet the feature tiling mode is not enabled with full resolution when supported. There is great blog on snapping, see the section on data requirements. Points aren't quantized the same way as polylines, making them safer to snap too. This is why you're seeing the snap source failing to be set for polylines.

myMap.LoadSettings.FeatureTilingMode = FeatureTilingMode.EnabledWithFullResolutionWhenSupported 

 

0 Kudos