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  


Handling Pushpin Collisions

Wilfried Mestdagh shares a solution for reducing the number of pushpins that must be displayed on a map

The question often comes up where thousands of pushpins must be displayed. However, nobody wants thousands of pushpins on the screen. So if you get that task then first rephrase the problem, that is: I have thousands pushpins, how do I make a comfortable GUI ?

Displaying thousands of pushpins take time. To avoid this you can only display the pushpins that are in view. Another point is the collision. You really don't want to have an ugly view of lots of pushpins overlapping each other.

In this article we describe the most simple thing to avoid collision, that is with sequentially positions as a route from a vehicle. In a future article we describe other possible approach needed for non sequentially positions.

You really don't want to display an ugly screen like the one above. Instead you calculate the distance between each pushpin, get the distance of 1 pixel, and leave preferable 20 pixels between each pushpin. This must be recalculated at every pan or zoom of the map.

This is the result you get, the more you zoom in you get more detail and more pushpins will be plotted. Only pushpins that are in view are plotted. This picture is the same data but with the code described and with a minimum distance of 20 pixels between the center of the pushpins.

You eventually can indicate to the user that there is more detail available when zooming in by setting a custom symbol for each pushpin if there are some not plotted because of collision, but these things are only limited to your own imagination.

Let's explain shortly what we going to do. First of all we calculate the geocoordinates of the 4 corners of the visible map. We need this to only plot pushpins that are in the visible area. So we calculate the top and bottom latitude and the left and right longitude to do that.

Location topLeftLoc = MP.ActiveMap.XYToLocation(0, 0);
Location bottomRightLoc = MP.ActiveMap.XYToLocation(MP.ActiveMap.Width, MP.ActiveMap.Height);
double top = topLeftLoc.Latitude;
double left = topLeftLoc.Longitude;
double bottom = bottomRightLoc.Latitude;
double right = bottomRightLoc.Longitude;

Then we have to specify the minimum distance we want between each pushpin. We can do that to ask MapPoint the distance of 1 pixel. We want at least 20 pixels between the center of our pushpins so we multiply with 20.

double minimumDistance = MP.ActiveMap.PixelSize * 20;

We do the calculation for each record. In this example dispLat and dispLon is the last displayed pushpin where we want a minimum distance from. You can put it in AfterViewChange then it is recalculated at every pan or zoom of the map.

double lat = positions.DataSource[positions.Index].Lat;
double lon = positions.DataSource[positions.Index].Lon;
if (lat <= top && lat >= bottom && lon >= left && lon <= right &&
    getDistance(dispLat, dispLon, lat, lon) >= minimumDistance) {
    dispLat = lat;
    dispLon = lon;
    Location loc = MP.ActiveMap.GetLocation(lat, lon, 1);
    MP.ActiveMap.AddPushpin(loc, positions.DataSource[positions.Index].Name);
}

We only need to explain the getDistance function. The example is for km. The cosine calculation is for correction of the distance in longitude because this is only equal to latitude if you are on the equator. In this example the distances are very short so this is only needed once and not in each call so you can speed it up. The 60 * 1.852 is because 1 arc minute is exact 1 nautical mile and the division by 1.609 is if your geo units are in miles instead of in kilometres.

private double getDistance(double lat1, double lon1, double lat2, double lon2)
{
    double lat = lat2 - lat1;
    double lon = (lon2 - lon1) * Math.Cos(Deg2Rad((lat1 + lat2) / 2));
    double dist = Math.Sqrt(Math.Pow(lat, 2) + Math.Pow(lon, 2));
    return dist * 60 * 1.852;
    //return dist * 60 * 1.852 / 1.609;
}

This is the CSV file from where I have got the data in this example and here is the complete code inclusive the reader and object data table I have used so that you can drop it in a VS project to test it. Just make a new project, drop an MapPoint activeX component on it, name it MP, assign the AfterViewChange and you can see it working.

In this example I delete all datasets on the map. You don't want this of course. I suggest to create a dataset for the route and only delete that particular dataset. But this is only a quick example of course. The data is in Belgium, but maybe someone can make a CSV file from a route somewhere in US and post the link here, so that the example is also usable with MapPoint North America version.

Discuss this story in the forum.

Author: Wilfried Mestdagh
Email: wilfried(AT)mestdagh.biz
Wilfried Mestdagh works as software engineer at the company Sonal in Mortsel, Belgium. His main work is writing software for fleet management and onboard computers. Fleet management is mainly written in Delphi and C# while the onboard computers are mostly programmed in C. His department started years ago for specializing in tracking and tracing security and dangerous transport vehicles, but it is grown to satisfy a very wide of vehicle / truck fleet customers.



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 Columbia MO 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