6

Just wondering if there are any decent resources on how to program a draggable pushpin for a map in a windows phone 7 application. I've had a good look and can only find information about how to do it for a browser application.

Ideally I want the user to be able to click on a pushpin and drag it to a location on the map however, at the minute the only way I can think of doing this is for the user to drag the map and the pushpin remains at the centre of the map.

James Mundy
  • 4,180
  • 6
  • 35
  • 58

3 Answers3

6

I've not seen this done on a WP7 app yet - but here is a description for Silverlight 3 - http://pietschsoft.com/post/2010/05/30/Draggable-Pushpins-using-Bing-Maps-Silverlight-Control.aspx

When implementing this I'd guess that you'd want to be careful about where you actually drop the icon in relation to your finger - e.g. if you look at how the text caret is moved in a text block when you click/hold/drag, then you'll see that the caret position is offset above the finger so that you can always see it.

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • thanks for the link, the issue with this is though that it makes reference to Microsoft.Maps.MapControl according to this link:http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/ded7a780-8f19-4411-9136-c17b84a11e1a you can't do that on the phone. – James Mundy Mar 15 '11 at 15:41
  • I think that link is out of date - the Bing Maps SDK has been available for several months now - http://msdn.microsoft.com/en-us/library/ff955762.aspx - I've even used it from IronRuby - http://script.iron7.com/#/Script/Detail?scriptId=Location&userLowerCaseName=iron7 – Stuart Mar 15 '11 at 15:46
  • I've been working on this since yesterday but I think I've got a solution that I devised myself. Basically, had a little play around with the map and gestures and worked out a method whereby you can find the exact position of a tap on the map and relate this to a real position on the map using the resolution of the image at a given latitude and a couple of astronomy formulae relating to ellipses and the distance between lines of latitude and longitude on the earth. – James Mundy Mar 16 '11 at 16:15
4

Yes you can. Here is a nice writeup how to implement this behavior.

Draggable PushPins

Greg Zimmers
  • 1,390
  • 10
  • 6
  • thanks also for the link but as I mentioned to Stuart above apparently there are some issues regarding including the Microsoft.Maps.MapControl in a Windows Phone 7 application – James Mundy Mar 15 '11 at 15:43
2

I was able to drag a Pushpin by adding an event handler for MouseMove and updating the Pushpin to the location of the mouse.

<my:Pushpin x:Name="pushpin" MouseLeftButtonDown="pushpin_MouseLeftButtonDown" MouseLeftButtonUp="pushpin_MouseLeftButtonUp" MouseMove="pushpin_MouseMove"/>

But the problem is the Map Control will also pan at the same time you are dragging the Pushpin. To solve that I had to add an event handler for mouse up and mouse down to the Pushpin and one for MapPan for the Map Control.

        private void mapControl_MapPan( object sender, MapDragEventArgs e )
        {
          if( isDragging )
          {
            e.Handled = true;
          }
        }

        private void pushpin_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
          pushpin.CaptureMouse( );
          isDragging = true;
        }

        private void pushpin_MouseLeftButtonUp( object sender, MouseButtonEventArgs e )
        {
          pushpin.ReleaseMouseCapture( );
          isDragging = false;
        }

        private void pushpin_MouseMove( object sender, MouseEventArgs e )
        {
          pushpin.Location = mapControl.ViewportPointToLocation( e.GetPosition( mapControl) );
        }

That will prevent the map from panning while the Pushpin is being dragged.

vee
  • 720
  • 7
  • 8