# Snapping help needed

**URL:** <https://community.thinkgeo.com/t/snapping-help-needed/881>\
**Category:** WinForms\
**Created:** [November 26, 2009, 11:20am UTC](https://community.thinkgeo.com/t/snapping-help-needed/881 "2009-11-26T11:20:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![John4](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@John4](https://community.thinkgeo.com/u/John4)\
**Post date:** [November 26, 2009, 11:20am UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/1 "2009-11-26T11:20:53Z")

</div>

Hi&nbsp;Guys,

I&nbsp;need&nbsp;a&nbsp;liitle&nbsp;help&nbsp;on&nbsp;snapping,&nbsp;I&nbsp;am&nbsp;almost&nbsp;there&nbsp;but&nbsp;just&nbsp;missing&nbsp;something.

Using&nbsp;the&nbsp;EditOverlay\_MapMouseUp,&nbsp;I&nbsp;have&nbsp;the&nbsp;vertex&nbsp;that&nbsp;was&nbsp;moved,&nbsp;I&nbsp;have&nbsp;the&nbsp;point&nbsp;to&nbsp;snap&nbsp;the&nbsp;vertex&nbsp;to&nbsp;in&nbsp;map&nbsp;coordinates.&nbsp;&nbsp;How&nbsp;do&nbsp;you&nbsp;move&nbsp;the&nbsp;vertex&nbsp;to&nbsp;the&nbsp;snapped&nbsp;point&nbsp;and&nbsp;then&nbsp;update&nbsp;the&nbsp;ControlPoints&nbsp;and&nbsp;the&nbsp;underlying&nbsp;data.

Been&nbsp;playing&nbsp;with&nbsp;this&nbsp;for&nbsp;hours&nbsp;now,&nbsp;the&nbsp;help&nbsp;contradicts&nbsp;itself,&nbsp;I&nbsp;can't&nbsp;find&nbsp;the&nbsp;MoveVertex&nbsp;call&nbsp;and&nbsp;am&nbsp;getting&nbsp;very&nbsp;frustrated,&nbsp;a&nbsp;half&nbsp;decent&nbsp;example&nbsp;would&nbsp;help!

Regards

John

&nbsp;

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [November 26, 2009, 8:36pm UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/2 "2009-11-26T20:36:40Z")

</div>

John,

I&nbsp;have&nbsp;created&nbsp;a&nbsp;sample&nbsp;to&nbsp;show&nbsp;how&nbsp;to&nbsp;extend&nbsp;EditOverlay&nbsp;and&nbsp;make&nbsp;it&nbsp;can&nbsp;snapped&nbsp;select&nbsp;a&nbsp;vertex.

Please&nbsp;let&nbsp;me&nbsp;know&nbsp;if&nbsp;you&nbsp;have&nbsp;questions

Thanks

James

[1500-SnappingEdit.zip](https://community.thinkgeo.com/uploads/default/original/2X/0/0f4cd899d78f3a03fbb2dd848c4c55eabca88eee.zip) (11.6 KB)

---

<div class="post-metadata">

**Author:** ![John4](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@John4](https://community.thinkgeo.com/u/John4)\
**Post date:** [November 27, 2009, 4:38am UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/3 "2009-11-27T04:38:03Z")

</div>

Hi&nbsp;James,

Thanks&nbsp;for&nbsp;that,&nbsp;not&nbsp;quite&nbsp;what&nbsp;I&nbsp;was&nbsp;expecting.

The&nbsp;objective&nbsp;of&nbsp;'snapping'&nbsp;whilst&nbsp;editing&nbsp;would&nbsp;be&nbsp;to&nbsp;'snap'&nbsp;or&nbsp;move&nbsp;a&nbsp;control&nbsp;point&nbsp;to&nbsp;the&nbsp;position&nbsp;of&nbsp;a&nbsp;point&nbsp;from&nbsp;another&nbsp;layer.

The&nbsp;source&nbsp;below&nbsp;shows&nbsp;a&nbsp;partially&nbsp;implemented&nbsp;soloution,&nbsp;we&nbsp;are&nbsp;just&nbsp;missing&nbsp;the&nbsp;'update'&nbsp;bit.

&nbsp;

`using System; using System.Collections.ObjectModel; using System.Drawing; using System.Windows.Forms; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.DesktopEdition; namespace SnappingEdit { public partial class Form1 : Form { private InMemoryFeatureLayer _snappingLines; private InMemoryFeatureLayer _snappingPoints; private LayerOverlay _staticOverlay; private InteractionArguments _args; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { winformsMap1.MapUnit = GeographyUnit.Meter; winformsMap1.CurrentExtent = new RectangleShape(0, 1000000, 1000000, 0); winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.StandardColors.White); _editor = winformsMap1.EditOverlay = _editor; _snappingLines = new InMemoryFeatureLayer(); _snappingLines.InternalFeatures.Add("Line", new Feature(BaseShape.CreateShapeFromWellKnownData( "LINESTRING(600000 600000, 700000 700000,750000 600000, 800000 700000, 850000 600000,950000 800000)"))); _snappingLines.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.Equator1; _snappingLines.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; _staticOverlay = new LayerOverlay(); _staticOverlay.Layers.Add(_snappingLines); _snappingPoints = new InMemoryFeatureLayer(); _snappingPoints.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData( "POINT(600000 600000)"))); _snappingPoints.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData( "POINT(700000 700000)"))); _snappingPoints.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData( "POINT(750000 600000)"))); _snappingPoints.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData( "POINT(800000 700000)"))); _snappingPoints.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData( "POINT(850000 600000)"))); _snappingPoints.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData( "POINT(950000 800000)"))); _snappingPoints.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City3; _snappingPoints.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; _staticOverlay.Layers.Add(_snappingPoints); winformsMap1.Overlays.Add(_staticOverlay); _editor.EditShapesLayer.InternalFeatures.Add("Line", new Feature(BaseShape.CreateShapeFromWellKnownData( "LINESTRING(800000 800000, 900000 900000,1000000 1000000)"))); _editor.CanAddVertex = false; _editor.CanDrag = false; _editor.CanRemoveVertex = false; _editor.CanResize = false; _editor.CanRotate = false; _editor.CalculateAllControlPoints(); _editor.MapMouseMove += _editor_MapMouseMove; _editor.VertexMoving += _editor_VertexMoving; winformsMap1.Refresh(); } void _editor_mapmousemove(object sender, mapmousemoveinteractiveoverlayeventargs e) { _args = e.interactionarguments; } private PointShape FindNearestSnappingPoint(PointShape aPos) { Int32 tolerance = (Int32)(winformsMap1.CurrentScale / 200); Collection<feature> wpts = _snappingPoints.QueryTools.GetFeaturesNearestTo(aPos, GeographyUnit.Meter, 1, ReturningColumnsType.NoColumns); int aDist = (int)aPos.GetDistanceTo(wpts[0].GetShape(), GeographyUnit.Meter, DistanceUnit.Meter); if (aDist < tolerance) return (PointShape)wpts[0].GetShape(); return null; } // The objective here is to allow any of the edited control points to 'snap' to any of the points in _snappingPoints void _editor_VertexMoving(object sender, VertexMovingEditInteractiveOverlayEventArgs e) { // Only do snapping if the shift key is used if(ModifierKeys == Keys.Shift) { // Find a point from the _snappingPoints layer that will be the target to snap to _snapToPoint = FindNearestSnappingPoint(new PointShape(_args.WorldX,_args.WorldY)); if (_snapToPoint != null) { // Here we know that we want to snap (move) the control point that is being dragged to the position of _snapPoint and update // the controlpoint position, the underrlying data and the mouse position } } } } } `

`Hope this helps to explain the problem`

`John`

---

<div class="post-metadata">

**Author:** ![Val](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/v/e56c9b/32.png) [@Val](https://community.thinkgeo.com/u/Val)\
**Post date:** [November 27, 2009, 3:56pm UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/4 "2009-11-27T15:56:04Z")

</div>

Today,&nbsp;we&nbsp;posted&nbsp;a&nbsp;new&nbsp;project&nbsp;in&nbsp;the&nbsp;Code&nbsp;Community&nbsp;that&nbsp;shows&nbsp;how&nbsp;to&nbsp;have&nbsp;the&nbsp;mouse&nbsp;pointer&nbsp;snapping&nbsp;to&nbsp;the&nbsp;nearest&nbsp;vertex&nbsp;for&nbsp;dragging&nbsp;it.&nbsp;It&nbsp;is&nbsp;not&nbsp;exactly&nbsp;what&nbsp;you&nbsp;are&nbsp;waiting&nbsp;for&nbsp;but&nbsp;early&nbsp;next&nbsp;week,&nbsp;we&nbsp;will&nbsp;work&nbsp;on&nbsp;an&nbsp;InteractiveOverlay&nbsp;for&nbsp;snapping&nbsp;vertices&nbsp;within&nbsp;a&nbsp;tolerance&nbsp;based&nbsp;on&nbsp;another&nbsp;layer&nbsp;as&nbsp;your&nbsp;expections&nbsp;are.&nbsp;Thank&nbsp;you.&nbsp;  
&nbsp;&nbsp;  
&nbsp;[code.thinkgeo.com/projects/show/snappingtovertex](http://code.thinkgeo.com/projects/show/snappingtovertex)&nbsp;

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [November 29, 2009, 9:46pm UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/5 "2009-11-29T21:46:02Z")

</div>

John,

I&nbsp;have&nbsp;updated&nbsp;your&nbsp;code&nbsp;and&nbsp;now&nbsp;is&nbsp;what&nbsp;you&nbsp;exactly&nbsp;want.

Begin,

 ![](https://community.thinkgeo.com/uploads/default/original/2X/8/81b95fe43ce06c4611c9f71c108eb3540dd4388d.png)

If&nbsp;you&nbsp;press&nbsp;the&nbsp;shift&nbsp;key&nbsp;and&nbsp;drag,&nbsp;it&nbsp;will&nbsp;snap&nbsp;to&nbsp;a&nbsp;point&nbsp;from&nbsp;another&nbsp;layer,

 ![](https://community.thinkgeo.com/uploads/default/original/2X/9/985e11e99e2506e77031c541d611d9d02a834ecf.png)

Please&nbsp;let&nbsp;me&nbsp;know&nbsp;if&nbsp;you&nbsp;have&nbsp;questions.

Thanks

James

[1503-SnapALayer.zip](https://community.thinkgeo.com/uploads/default/original/2X/4/4822bbc7f27cfafed030e1b1eda6b4eed3781c60.zip) (11.3 KB)

---

<div class="post-metadata">

**Author:** ![John4](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@John4](https://community.thinkgeo.com/u/John4)\
**Post date:** [November 30, 2009, 6:14am UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/6 "2009-11-30T06:14:17Z")

</div>

Hi&nbsp;James,

That's&nbsp;perfect,&nbsp;just&nbsp;what&nbsp;I&nbsp;was&nbsp;looking&nbsp;for.

John

&nbsp;

---

<div class="post-metadata">

**Author:** ![ThinkGeo](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/thinkgeo/32/25029_2.png) [@ThinkGeo](https://community.thinkgeo.com/u/ThinkGeo)\
**Post date:** [November 30, 2009, 7:11pm UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/7 "2009-11-30T19:11:33Z")

</div>

John,&nbsp;  
&nbsp;&nbsp;  
&nbsp;It’s&nbsp;awesome&nbsp;you&nbsp;got&nbsp;it.&nbsp;  
&nbsp;&nbsp;  
&nbsp;James

---

<div class="post-metadata">

**Author:** ![Arber](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@Arber](https://community.thinkgeo.com/u/Arber)\
**Post date:** [June 10, 2012, 8:25am UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/8 "2012-06-10T08:25:04Z")

</div>

Hello,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;integrated&nbsp;the&nbsp;above&nbsp;code&nbsp;in&nbsp;my&nbsp;project&nbsp;and&nbsp;it&nbsp;is&nbsp;not&nbsp;working&nbsp;with&nbsp;the&nbsp;  
&nbsp;`e.MovingVertex=New Vertex(_snapToPoint) `&nbsp;  
&nbsp;but&nbsp;it&nbsp;works&nbsp;with&nbsp;  
&nbsp;`e.TargetVertex=New Vertex(_snapToPoint)  `&nbsp;  
&nbsp;while&nbsp;in&nbsp;the&nbsp;provided&nbsp;solution&nbsp;above&nbsp;it&nbsp;works&nbsp;fine&nbsp;without&nbsp;changing&nbsp;anything.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Can&nbsp;you&nbsp;please&nbsp;explain&nbsp;the&nbsp;difference&nbsp;between&nbsp;MovingVertex&nbsp;and&nbsp;TargetVertex&nbsp;because&nbsp;it&nbsp;is&nbsp;not&nbsp;very&nbsp;clear&nbsp;in&nbsp;the&nbsp;documentation?&nbsp;  
&nbsp;&nbsp;  
&nbsp;Regards,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Arber

---

<div class="post-metadata">

**Author:** ![Gary2](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@Gary2](https://community.thinkgeo.com/u/Gary2)\
**Post date:** [June 21, 2012, 9:08am UTC](https://community.thinkgeo.com/t/snapping-help-needed/881/9 "2012-06-21T09:08:09Z")

</div>

Hello&nbsp;Arber,&nbsp;  
&nbsp;&nbsp;  
&nbsp;“MovingVertex”&nbsp;and&nbsp;“TargetVertex”&nbsp;are&nbsp;all&nbsp;in&nbsp;“MoveVertexCore”.&nbsp;The&nbsp;“TargetVertex”&nbsp;means&nbsp;a&nbsp;target&nbsp;point&nbsp;when&nbsp;the&nbsp;vertex&nbsp;was&nbsp;moved,&nbsp;but&nbsp;the&nbsp;“MovingVertex”&nbsp;was&nbsp;sent&nbsp;to&nbsp;user&nbsp;codes&nbsp;and&nbsp;the&nbsp;user&nbsp;can&nbsp;modify&nbsp;“MovingVertex”&nbsp;when&nbsp;the&nbsp;VertexMoving&nbsp;event&nbsp;was&nbsp;raised.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Regards,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Gary
