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 2009
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 2009

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
ViaVirtualEarth
SVG Tutorials
MapPoint

Map Visitors

 Direct Media

Travel Insurance Compare over 450 travel insurance policies (inc. annual multi-trip, single trip & winter sports)

  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
-bingmaps: Join the UK #BingMaps team for the finance and insurance sector webcast tomorrow http://bit.ly/chMYcb
-Bing Maps at MIX10!
-Bing Maps at MIX10!
-bingmaps: Want live Twitter and Maps on your website, use the new widget from Bing Maps: http://bit.ly/9KkFSa
-linked files
-Criteria for extracting postcodes in Mappoint Euro Ver
-Spatial Data Import COM Add-in / Shapes (SHP file)
-BingMapsDev: @jont87 For Silverlight I would start here: http://is.gd/a4BAq and then look at the Bing Maps iSDK: http://is.gd/a4C4y
-bingmaps: Channel 5's Gadget Show are impressed by #BingMaps Birds Eye (forward to 24 mins) http://bit.ly/9Cn6sY
-bingmaps: #BingMaps SXSW Panel Announced http://bit.ly/9GWSeT
-bingmaps: Bing Maps retail and supply chain webcast starts in 10 minutes. Register here: http://bit.ly/dsNNJD
-Bing Maps SXSW Panel Announced
-Check if Mappoint exists on the PC
-bingmaps: @markti Great glad you like it!
-bingmaps: RT @ChrisPendleton: Bing Maps adds 6.7 MILLION sq. km. of new imagery: http://bit.ly/bCcyCw. @BingMaps #Russia #Sweden #Mexico #Australia
-bingmaps: #BingMaps UK team present Mapping Technology for the Communications sector http://cot.ag/979oFy
-bingmaps: Johannes Kebeck blogs on Daimler AG and the Smart Web Portal in the Cloud http://bit.ly/a8uvsR #BingMaps
-BingMapsDev: Have you joined the Bing Maps UK user group yet? http://bingmapsuk.ning.com/
-WiMax Clear Service in Chicago
-Registry Error Installing 2006 Version in Windows 7 Professional
-MapPoint Geocoding Project
-Water Feature toggle
-objGPS.Tracking = True give access denied
-Smart Web Portal in the Cloud
-The latest Bing Maps Imagery Update…
 Resources
Browse GIS books and periodicals
Best Car Contract Hire Lease Save
Find a MapPoint Partner or Consultant
Real Estate Thornbrook Homes for Sale


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.

• Replacement Cordless Phone Batteries from DSMiller
Improve your career with a computer degree LocalEDU.com
• Tim Cohn's Marketing Strategy Consultancy
• Buy Toner Cartridges at Marquee Office Solutions
• Looking for Dry Erase Boards? See Dry Erase Boards • The Search Starts Here Used Cars
• 5339 - We Know Bikes: Road Bikes / Triathlon Bikes / Mountain Bikes / BMX Bikes
• Buy Your Car Used Cars UK, Car Lease and Contract Hire