Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I added a drag handle module to the polygon in order to edit each point in the polygon. This is the walk-through I used.</p> <p><a href="http://www.garzilla.net/vemaps/Bing-Maps-7-Modules-Drag-Handles.aspx" rel="nofollow">Drag Handle Module</a></p> <p>This is for version 7.0. Not sure which version you are using, but it is easily adaptable. </p> <p><strong>Edit</strong> I did this in javascript/html. Just noticed you tagged this as windows 8. Curious, are you making a silver-light control for windows 8 phones?</p> <p><strong>WPF C# Solution</strong></p> <p>Here is what I came up with and it is very easy to implement, all you need to do is hook my event onto your <code>MapPolygon</code> object. The idea behind the code, is once you click on the polygon, we place a pushpin (which we create events so that we can drag it) at each vertice on the polygon. As we drag and drop each pushpin we adjust the polygon.Locations as applicable. </p> <pre><code>polygon.MouseDown += new MouseButtonEventHandler(polygon_MouseDownResize); </code></pre> <p>And a way to exit the resize event, I choose to just hook onto the key down and used 'esc' to stop editing, but you can do this on right click or any other event you choose to. All your doing is clearing <code>pushPinVertices</code> list and resetting event variables.</p> <pre><code>// === Editable polygon Events === private bool _shapeEdit; public MapPolygon selectedPoly { get; set; } public List&lt;Pushpin&gt; pushPinVertices { get; set; } void polygon_MouseDownResize(object sender, MouseButtonEventArgs e) { if (!_shapeEdit &amp;&amp; selectedPoly == null) { _shapeEdit = true; selectedPoly = sender as MapPolygon; pushPinVertices = new List&lt;Pushpin&gt;(); int i = 0; foreach (Microsoft.Maps.MapControl.WPF.Location vertice in selectedPoly.Locations) { Pushpin verticeBlock = new Pushpin(); // I use a template to place a 'vertice marker' instead of a pushpin, il provide resource below verticeBlock.Template = (ControlTemplate)Application.Current.Resources["PushPinTemplate"]; verticeBlock.Content = "vertice"; verticeBlock.Location = vertice; verticeBlock.MouseDown += new MouseButtonEventHandler(pin_MouseDown); verticeBlock.MouseUp += new MouseButtonEventHandler(pin_MouseUp); myMap.Children.Add(verticeBlock); pushPinVertices.Add(verticeBlock); i++; } } } private void myMap_KeyDown(object sender, KeyEventArgs e) { if (e.Key == System.Windows.Input.Key.Escape) { if (_shapeEdit &amp;&amp; selectedPoly != null) { foreach (Pushpin p in pushPinVertices) { myMap.Children.Remove(p); } _shapeEdit = false; selectedPoly = null; } } } // Note: I needed my window to pass bing maps the keydown event private void Window_KeyDown(object sender, KeyEventArgs e) { myMap_KeyDown(sender, e); } // === Editable polygon Events === // ==== Draggable pushpin events ===== Vector _mouseToMarker; private bool _IsPinDragging; public Pushpin SelectedPushpin { get; set; } void pin_MouseUp(object sender, MouseButtonEventArgs e) { LocationCollection locCol = new LocationCollection(); foreach (Pushpin p in pushPinVertices) { locCol.Add(p.Location); } selectedPoly.Locations = locCol; bingMapRefresh(); _IsPinDragging = false; SelectedPushpin = null; } void pin_MouseDown(object sender, MouseButtonEventArgs e) { e.Handled = true; SelectedPushpin = (Pushpin)sender; _IsPinDragging = true; _mouseToMarker = Point.Subtract( myMap.LocationToViewportPoint(SelectedPushpin.Location), e.GetPosition(myMap)); } private void myMap_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { if (_IsPinDragging &amp;&amp; SelectedPushpin != null) { SelectedPushpin.Location = myMap.ViewportPointToLocation(Point.Add(e.GetPosition(myMap), _mouseToMarker)); e.Handled = true; } } } // ==== Draggable pushpin events ===== // Nice little maprefresh I found online since the bingmap WPF doesnt always seem to update elements after certain event orders private void bingMapRefresh() { //myMap.UpdateLayout(); var mapCenter = myMap.Center; mapCenter.Latitude += 0.00001; myMap.SetView(mapCenter, myMap.ZoomLevel); } </code></pre> <p>As for the resource to overwrite the pushpin icon, I just used an <code>ImageBrush</code> an made a small white square. Note you may need to adjust the <code>Margin</code> property depending on how long / wide you make your marker. This is because generally the pushpin is anchored above the location by default.</p> <pre><code>&lt;Application.Resources&gt; &lt;ControlTemplate x:Key="PushPinTemplate"&gt; &lt;Grid&gt; &lt;Rectangle Width="10" Height="10" Margin="0 35 0 0"&gt; &lt;Rectangle.Fill&gt; &lt;ImageBrush ImageSource="pack://application:,,,/Images/DragIcon.gif"/&gt; &lt;/Rectangle.Fill&gt; &lt;/Rectangle&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Application.Resources&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload