Draggable Pushpins using Bing Maps Silverlight Control

30. May 2010

Using a map to visualize data within an application is great, but you must first get the location of the data to be displayed. If you have the address you can geocode it using the Bing Maps Web Services, but "What if you can't geocode it?" Or, "What if the geocoding can't find the address?" Well, if your user knows where the location is, then you can have them point it out by clicking on the map. Creating Pushpins in response to a users click is nice, but wouldn't it be even nicer if they could "Click and Drag" the Pushpin around to define/edit/change the location of the data entity?

I have even seen this discussed a bit in regards to the Bing Maps Silverlight Control, and it isn't something that is built into the map control directly. However it isn't too difficult to implement, if you know what to do. So I decided to create and post a simple "DraggablePushpin" object deriving from the "Microsoft.Maps.MapControl.Pushpin" object that implements Dragging in a nice, self contained fashion. There's no need to wire up any events, you simple add a "DraggablePushpin" to you Map, and the user can drag it around.

Here's the code for the "DraggablePushpin":

public class DraggablePushpin : Microsoft.Maps.MapControl.Pushpin
{
    private bool isDragging = false;
    EventHandler<MapMouseDragEventArgs> ParentMapMousePanHandler;
    MouseButtonEventHandler ParentMapMouseLeftButtonUpHandler;
    MouseEventHandler ParentMapMouseMoveHandler;

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        // Check if the Map Event Handlers have been created/attached to the Map
        // If not, then attach them. This is done in the "Pushpin.OnMouseLeftButtonDown"
        // event because we don't know when the Pushpin is added to a Map or MapLayer, but
        // we do konw that when this event is fired the Pushpin will already have been added.
        var parentLayer = this.Parent as MapLayer;
        if (parentLayer != null)
        {
            var parentMap = parentLayer.ParentMap;
            if (parentMap != null)
            {
                if (this.ParentMapMousePanHandler == null)
                {
                    this.ParentMapMousePanHandler = new EventHandler<MapMouseDragEventArgs>(ParentMap_MousePan);
                    parentMap.MousePan += this.ParentMapMousePanHandler;
                }
                if (this.ParentMapMouseLeftButtonUpHandler == null)
                {
                    this.ParentMapMouseLeftButtonUpHandler = new MouseButtonEventHandler(ParentMap_MouseLeftButtonUp);
                    parentMap.MouseLeftButtonUp += this.ParentMapMouseLeftButtonUpHandler;
                }
                if (this.ParentMapMouseMoveHandler == null)
                {
                    this.ParentMapMouseMoveHandler = new MouseEventHandler(ParentMap_MouseMove);
                    parentMap.MouseMove += this.ParentMapMouseMoveHandler;
                }
            }
        }

        // Enable Dragging
        this.isDragging = true;

        base.OnMouseLeftButtonDown(e);
    }

    #region "Mouse Event Handler Methods"

    void ParentMap_MousePan(object sender, MapMouseDragEventArgs e)
    {
        // If the Pushpin is being dragged, specify that the Map's MousePan
        // event is handled. This is to suppress the Panning of the Map that
        // is done when the mouse drags the map.
        if (this.isDragging)
        {
            e.Handled = true;
        }
    }

    void ParentMap_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        // Left Mouse Button released, stop dragging the Pushpin
        this.isDragging = false;
    }

    void ParentMap_MouseMove(object sender, MouseEventArgs e)
    {
        var map = sender as Microsoft.Maps.MapControl.Map;
        // Check if the user is currently dragging the Pushpin
        if (this.isDragging)
        {
            // If so, the Move the Pushpin to where the Mouse is.
            var mouseMapPosition = e.GetPosition(map);
            var mouseGeocode = map.ViewportPointToLocation(mouseMapPosition);
            this.Location = mouseGeocode;
        }
    }

    #endregion
}

Bing Maps, Silverlight

Comments

6/4/2010 12:27:29 PM #
Thanks for posting!
6/5/2010 4:37:20 PM #
when i learning and i just google blog,blog is a good place to learning,i am glad to found your blog
6/6/2010 6:48:40 AM #
This is very interesting. Keep blogging the way you do Smile
6/6/2010 7:38:31 AM #
One of the great features about blogs is the capability to authorize all of us to share many thoughts on the World Wide Web. Thank you for sharing yours.

6/6/2010 1:02:50 PM #
This is very interesting. I actually enjoy your writing style and your word choice more than anything Smile
6/6/2010 1:18:26 PM #
I am sharing this with my spouse. Maybe this will get him interested with me again. Things have been a little boring lately, hopefully this will change that.
6/6/2010 3:04:55 PM #
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
6/6/2010 7:52:11 PM #
I have frequently stumbledupon your port before. The more I visit, the more I keep coming back! Wink
6/7/2010 4:25:58 AM #
This is very interesting. I actually enjoy your writing style and your word choice more than anything Smile
6/7/2010 8:45:41 AM #
I wish everyone would do this, it would make life so much easier.
6/8/2010 10:46:15 AM #
Thanks for the informative article. Will check out more of your blog posts!
6/8/2010 11:15:59 AM #
wat the
6/8/2010 5:59:12 PM #
Great post
Dru_MS
Dru_MS
6/8/2010 9:06:13 PM #
Hi,
looks great.
Do you think it´s possible to realize it with polylines and polygons ,too?
It would be great if it´s possible to edit polylines and polygons by dragging the vertices ?!
6/8/2010 9:29:36 PM #
Nice post. I will post this into my twitter account.
6/9/2010 12:32:05 AM #
Thought you may like to know I came to this webpage from the first page of aAsk seach. Hard Work. I know how tidious it is to get your this page on the first page of a search. Ive downloaded program after program and now got <A href="http://nurfguns.net/">Buzzbee Shotgun</A></SPAN></SPAN></SPAN> on the first page. sheesh only took 3weeks!!
6/9/2010 2:39:55 AM #
I cant believe the multitude of wonderful info you have on your blog. I have learned a lot from it. Will be coming back soon.
6/9/2010 3:17:52 AM #
Greetings from Kansas!  Just googled your posts.  Actually took in your article, I'll pass it along!  =D  Have a fantastic day!
6/9/2010 3:22:54 PM #
Pretty good post.I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope. you post again soon.
6/9/2010 5:25:11 PM #
Hi. Appreciation for an enjoyable blog site post. My spouse and I found it pretty professional and am optimistic to keep coming back soon.
6/9/2010 8:48:48 PM #
Where can I buy senuke?
6/10/2010 12:16:05 AM #
I've been to China travel, in my opinion chinese people are very freidly, and i especially like the city of beijingshenzhen, i have a friend from china, i ike her blog discount china, now she is in Jewish, just now, she send me a Jewish Directory website, i think it is very useful.
6/10/2010 3:35:35 AM #
Did you know that I came to this page from the first page of aAsk seach. Great Job. I know how tidious it is to get your your site on the first page of a search. Ive purchased program after program and finally got <A href="http://free-music-down-loads.net/">Country Music Top 40</A> on the first page. sheesh only took 4weeks!!
6/10/2010 7:16:51 PM #
Hey very good blog!!!! Wow... Gorgeous .. Amazing .. I’ll bookmark your weblog and take the feeds also...
6/10/2010 9:06:18 PM #
Easy to read, easy to understand. Very rare to find.
6/10/2010 10:20:25 PM #
Completely understand what your stance in this matter. Although I would disagree on some of the finer details, I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
6/11/2010 12:28:19 AM #
I adore every information you have here. Glad you've shared this. Thanks! Smile
6/11/2010 3:09:14 AM #
I enjoy your blog, Its very good to find not all people is just posting a lot of trash these days!
6/11/2010 9:43:25 AM #
Hi i must say very nice blog.
6/11/2010 9:44:55 PM #
Haha Ive ordered it and noticed quite a big change in energy use to I could only run a mile now I can run 2 miles and still want to run more...and thats after 3 days of taking it....As for the guy that says look at all the 5 star ratings they only have 1 review so do 4 of the 5 1 star rating also hah! so it would seem that other companies are making accounts and reviewing this item so they can sell theres instead....hah this review buisness all seems like crap to me...hah but either way I took it and love itSmile havent even had it a week and ive felt more motivated than ever...im 19 and almost bald hah! funny stuff huh well my hair is starting to grow back already also..*no joke*;) anyways all these reviews are hard to believe is my point so really the only thing you can do is try it yourself and dont judge it by reviews cause reviews= fail in this case
6/11/2010 10:12:42 PM #
Seriously liked this one particular, maintain up the perfect writing!
6/11/2010 10:21:16 PM #
this is some good stuff man!
6/11/2010 11:16:12 PM #
interesting reading your articles- good things, that i forget and you write about them
6/12/2010 12:03:26 AM #
Hey very good blog!!!! Wow... Gorgeous .. Amazing .. I’ll bookmark your weblog and take the feeds also...
6/12/2010 3:48:23 AM #
Hey very good blog!!!! Wow... Gorgeous .. Amazing .. I’ll bookmark your weblog and take the feeds also...
6/12/2010 5:42:32 AM #
Hey very good blog!!!! Wow... Gorgeous .. Amazing .. I’ll bookmark your weblog and take the feeds also...
6/12/2010 8:33:11 AM #
I like your writing style, many thanks for that publish.
6/12/2010 9:21:43 AM #
Article very interesting.
6/12/2010 1:31:35 PM #
Hey very good blog!!!! Wow... Gorgeous .. Amazing .. I’ll bookmark your weblog and take the feeds also...
6/12/2010 9:16:07 PM #
I don't believe this! Smile
6/12/2010 9:44:27 PM #
Great post.  I really enjoyed it!
6/12/2010 11:30:19 PM #
Greetings from Kansas!  Just visited your website.  Actually visited your article, I'll email it along!  >:-o  Have a great day!
6/13/2010 12:59:16 AM #
Great post.  I really enjoyed it and I've linked to it from my blog.  Thanks!
6/13/2010 3:13:32 AM #
Hey there this is a wonderful post. I'm going to e-mail this to my buddies. I came on this while browsing on yahoo I'll be sure to come back. thanks for sharing.
6/13/2010 4:01:06 AM #
Go to http://fatherday2010.com for fantastic gifts that your dear father will enjoy!
6/13/2010 4:47:05 AM #
Thought you may like to know I came to this webpage from the first page of aAsk seach. Hard Work. I know how tidious it is to get your your website on the first page of a search. Ive downloaded ebook after ebook and now got <A href="downloadable-mp3-music.org/">Downloadable mp3 music</A></SPAN></SPAN></SPAN> on the first page. omg only took 4weeks!!
9/21/2010 1:21:12 AM #
Pingback from codeease.com

CodeEase.com  » Blog Archive   » Extend Bing Maps Silverlight
10/10/2011 12:11:29 AM #
Pingback from programmersgoodies.com

Draggable AND clickable pushpin in silverlight bing map - Programmers Goodies