spacer1
spacer2 1_1 1_2
2_1
 Subscribe
 The MP2K Update!
 
 
 
 Magazine
Front Cover
What's New
Articles
News
Sample Data
Gallery
Advertise
About
 Features
MapPoint 2011
Press Releases
MapPoint Forums
Companies
Link to MP2Kmag
Wish List
MapPoint Trial
Authors
 Earlier Content
Past News Items
Past What's New Announcements
 Sponsors
 Order

MapPoint 2011

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
SVG Tutorials
MapPoint

Map Visitors

  ARTICLES  


Making Pushpin Locations Immoveable

Fernando Velasquez shares a solution for preventing a user from moving pushpins on a map

Recently, I came up against the problem of preventing a user from dragging around the map certain pushpins created programmatically. The idea behind this is that it is sometimes useful t o prevent the pushpins from being moved because afterwards the user may not remember where the pushpin was originally and the whole idea of representing information in a map loses its value.

What gave me a clue about how it could be done easily is the way in which one carries out the action of moving a pushpin around the map. The dragging action of a pushpin differs slightly from the dragging action we can carry out for a desktop icon, in which you only need to click on it and start dragging. In MapPoint, this is a two-step process. First, you need to click on the pushpin for it to be selected, and then you need to click and hold on it to start dragging it around. So, armed with this knowledge, I started out seeking a way to interrupt this process.

The Solution

The solution I found involves doing some actions on the map in the middle of the dragging process. By middle I mean the interval between the mouse click that selected the pushpin and the mouse click that will start the dragging operation. Since I have found it impossible to move a pushpin by making the mouse clicks real quick, meaning there is a short time span that has to pass before being able to drag the pushpin, this gave me the idea that I could do a lot of things in that interval.

The main purpose of doing some actions in the interval mentioned above is to have a way to make it impossible for the user to accomplish the drag by just click and holding the selected pushpin, so the easiest way to do this is just selecting another object just as soon as the pushpin we don’t want moved is selected. MapPoint exposes an event called SelectionChange, which the help documentation states "Occurs when a selection on the map changes." This is the event that will do the magic for us.

In simple terms, what the code below does is detect when the pushpin we don’t want moved is selected, create another selectable object in a location that will not be easily seen by the user, select this new object and then delete it from the map, thus effectively shifting the current selection from the pushpin and preventing the user from dragging it around.

VB.Net Code

Line 5 checks to see if the new selection made is of type MapPoint.Pushpin; if you are looking to restrict a specific pushpin then more processing will need to be done, but for now I’ll focus on restricting movement to anything that is a pushpin on the map. If the selection is a pushpin, then a location object will be created. What is special about this location object is the place it is being created: Latitude 80, Longitude 0 represents a place very far away, it is located somewhere in the Arctic Ocean. You can actually choose any location you want, but the important thing to keep in mind is that it should be a place where it is not near any of the locations you are currently working with. We’ll see why next.

Lines 7-9 create a rectangle shape in the map, with its center located in the Arctic Ocean, and with a width and height of 1. It is important that the rectangle is of small size so the user won’t be able to notice the "magic" we are doing to keep the pushpins locked, and in the case that we don’t pay attention to the shape’s size, then the fact that it will be drawn far away from where the user is looking at will also help to achieve the desired effect. Personally, I think a combination of a far away object with a small size is the best one because there may be cases where the user could be looking at the whole globe, so this would minimize any traces of what has been done.

And that’s it! Now the user as much as he/she tries won’t be able to move anything that is a pushpin around the map, unless through the event handler we give permission to do so.

A final note: there are two VERY important remarks in the documentation for the SelectionChange event. One states that you shouldn’t create any modal dialog boxes or message boxes inside this event; since we don’t do that in the code then that’s not a problem, but the next one is. It says that we should NOT make that event infinitely recursive, either by calling itself or causing itself to be called. This is the main reason a shape object and the if statement in line 5 were used. Because the shape object is being programmatically selected, this could result in the SelectionChange event being raised, naturally creating a recursive call of itself, but since we first check to see if the newly selected object is a pushpin before doing anything, then the call will not advance and the normal flow will be returned when the code comes back from the recursion.

Bonus:

The above code was written to work under VB.Net, so I've included the code for VB6 here as well:

The logic is the same and the changes in code are minimal.

Discuss this story in the forum.

Author: Fernando Velasquez
Email: fvelasquez(AT)realsystemsinc.com
URL: http://www.realsystemsinc.com
Fernando works as a project manager for Real Systems, Inc. and also develops VB.Net-based applications using MapPoint. He lives in Tegucigalpa, Honduras (14.09489N, 87.20755W) and he enjoys sleeping, eating, reading books and solving complex computational problems using genetic programming.



Google
 
MP2Kmag Internet


 Recent Discussion
-MP 2011 Silent Install with Key AND Activation?
-Convert .GmL to a spatial data type
-bingmaps: Spouse run interference on an Indianapolis road trip for Super Bowl? Get the best view on Bing Maps. BYO beer & chips*http://t.co/hkZlFnJ4
-MAP POINT Route Planning
-MapPoint Routes: Multiple start points, one stop point.
-Export Territories - plot them on google maps or bing
-bingmaps: Check out this blog and interview to learn more about LifeLens and Bing Maps http://t.co/mlNHyKdW
-Can't make working OLAP add-in
-Importing data from Canada?
-MapPoint North America 2011 - UK Postcodes?
-Seeking Church Demographic info for MP2011
-Exporting Pushpin Counts in a radius
-bingmaps: RT @bing: Congrats to the Imagine Cup Grant Winners, esp. OaSys and LifeLens using @Bing maps! http://t.co/Fha9Cf0V
-Store Distance Calculations
-Modify maps
-Win7x64SP1 clobbers MapPoint 2010....
-bingmaps: RT @ChrisPendleton: New Bing Maps Features Help You Feel Spatial…
-Hope it doenst soudn TOO much like Im a Noob.. BUT...
-Capture click events from MapPoint to my Winforms app?
-bingmaps: @natelawrence Thanks for the feedback...check out our*Forum for technical assistance:*http://t.co/WDAjNJCD
-bingmaps: @Jordanaous*We're glad you're enjoying Streetside...thanks for using Bing Maps!
-bingmaps: @Goodlett*A beautiful partnership, indeed.
-bingmaps: @urbanmapping Love the fascinating questions you guys are asking, and the important information you provide. Keep it up!
-bingmaps: @beeinthehive*Happy to help! Thanks for using Bing Maps!
-bingmaps: @Geocrusader80* Isn't the imagery amazing? Glad you're a fan!
 Resources
Browse GIS books and periodicals
Find a MapPoint Partner or Consultant
Real Estate Thornbrook Subdivision


Want Your Site To Appear Here?

   © 1999-2009 MP2K. Questions and comments to: website@mp2kmag.com
  Microsoft and MapPoint 2002/2004/2006/2009 are either trademarks or registered trademarks of Microsoft.



• Buy Your Car Used Cars UK, Car Lease and Contract Hire