Drivetime Polygons with MapPoint and Bing Maps

There was a time, a few years ago, when the MapPoint CD could regularly be found in my computer’s CD drive… back then, it was the best tool for examining maps, plotting routes, and creating your own sets of pushpin data.

Nowadays, Bing maps, Google maps, and other online web-mapping providers provide much of the functionality that could previously only be found in desktop applications such as MapPoint, for free, with continuously updated road and aerial imagery data. The terabytes of global data that sit behind web applications such as Bing Maps could never easily be packaged up and distributed in a desktop application any more. And since these webservices are readily available from any web browser, (including almost all new mobile phones), there are less obvious reasons why you’d still want to use the MapPoint desktop application on your computer.

However, there are still some functions that MapPoint provides that are not easily available in Bing Maps or Google Maps (or, as far as I’m aware, any other web-mapping providers). One of these features is the ability to plot drivetime polygons – the area containing all those places that can be reached by road within a certain amount of time starting from a particular location.

Fortunately, MapPoint can be accessed through a COM interface, which means that we can create a drivetime polygon programmatically through code, and then export it as a SQL Server geography polygon or Bing Maps polygon to display using the Bing Maps control.

To do so, you first need to import a reference to the MapPoint Object Library into your application. Create a new Visual Studio project, select Add Reference from the Project menu, select the COM tab, and scroll down to select the Microsoft MapPoint Object Library.

image

Note that I’m using Microsoft MapPoint Europe 2010, so my library is version 17.0 – if you have a different version installed on your computer then the name of the library will differ accordingly, but you should still be able to use the same code.

Once you’ve imported the COM assembly, you can access the MapPoint map functions via the ApplicationClass of the MapPoint namespace. The following code demonstrates a simple C# console application that initialises the MapPoint application, creates a drivetime polygon that determines all locations that can be reached within 60 minutes drive from “10 Downing Street, London”, and then retrieves the points in those shapes and converts them to Well-Known Text:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            MapPoint.ApplicationClass app = new MapPoint.ApplicationClass();
            MapPoint.Map map = app.ActiveMap;

            object index = 1;
            MapPoint.Location location =
       (MapPoint.Location)map.FindResults("10 Downing Street, London, England").get_Item(ref index);

            //Add a 60 minute drivetime zone around a location
            MapPoint.Shape shape =
               map.Shapes.AddDrivetimeZone(location,
                          60 * MapPoint.GeoTimeConstants.geoOneMinute);

            string WKT = "POLYGON((";

            //Now get the vertices for this polygon
            object[] vertices = shape.Vertices as object[];
            foreach (object vertex in vertices)
            {
                MapPoint.Location loc = vertex as MapPoint.Location;
                WKT = WKT + loc.Longitude.ToString() + " " + loc.Latitude.ToString() + ", ";
            }
            WKT = WKT.Substring(0, WKT.Length - 2);
            WKT = WKT + "))";
            Console.Write(WKT);
        }
    }
}

Drivetime polygons created using this method can be stored in SQL Server 2008’s geography datatype, and then retrieved and plotted on Bing Maps.

Here’s some drivetime polygons generated using the method described above illustrating those areas reachable within  20 minutes, 40 minutes, and 60 minutes drive starting from my house in Norwich, displayed on Bing Maps v7:

image

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MapPoint.ApplicationClass app = new MapPoint.ApplicationClass();
            MapPoint.Map map = app.ActiveMap;
            object index = 1;
            MapPoint.Location location =
       (MapPoint.Location)map.FindResults("10 Downing Street, London, England").get_Item(ref index);
            //Add a 60 minute drivetime zone around a location
            MapPoint.Shape shape =
               map.Shapes.AddDrivetimeZone(location,
                          60 * MapPoint.GeoTimeConstants.geoOneMinute);
            string WKT = "POLYGON((";
            //Now get the vertices for this polygon
            object[] vertices = shape.Vertices as object[];
            foreach (object vertex in vertices)
            {
                MapPoint.Location loc = vertex as MapPoint.Location;
                WKT = WKT + loc.Longitude.ToString() + " " + loc.Latitude.ToString() + ", ";
            }
            WKT = WKT.Substring(0, WKT.Length - 2);
            WKT = WKT + "))";
            Console.Write(WKT);
        }
    }
}
This entry was posted in Bing Maps and tagged , , , . Bookmark the permalink.

12 Responses to Drivetime Polygons with MapPoint and Bing Maps

  1. MapForums says:

    Nicely done, but I would argue the drivetime polyons are one of the most worthless things in MapPoint. The polygons just aren’t very good.

    The import data wizard options for mapping spatial data whether by address or lat/lon or to any of the built-in geographies, from countries down to postal codes/small census boundaries with charts, multiple symbols, etc. maps is probably one of the most compelling things about the software.

    That, and the easy COM integration you mentioned and ease of developing with Office VBA.

    Anyway, nice article good work! I will bookmark for the C# example. By the way, I wrote an article for MSDN last Fall with more SQL Server/MapPoint examples you might like if you haven’t seen it
    http://msdn.microsoft.com/en-us/magazine/ff959816.aspx

    best,
    Eric
    http://www.MapForums.com

  2. Steve Horn says:

    Hello.
    I tried the code with some success, however when I try to create the polygon using the SQL Server API, I get this message:

    The Polygon input is not valid because the start and end points of the ring are not the same. Each ring of a polygon must have the same start and end points.

    Do you know why I might be getting the beginning and end points out of order in my WKT?

    Thanks.

  3. alastaira says:

    Steve – I’ve not had that problem before – my MapPoint polygons always appear to be closed. Are you using the same version of MapPoint as me? Looking at the WKT string generated, are the first and last point significantly different from each other, or is it just a rounding error?
    You could try pushing the first location in the vertices array onto the end of the WKT, i.e. immediately after the foreach loop, add:
    MapPoint.Location loc = vertices[0] as MapPoint.Location;
    WKT = WKT + loc.Longitude.ToString() + ” ” + loc.Latitude.ToString() + “, “;

    • Steve Horn says:

      Alastaira, sorry for the late reply 🙂

      Your suggestion is exactly what I ended up doing. I just took the first point and copied it to the last. Not sure what the problem was! I’m using Mappoint North America 2011.

  4. Jeff says:

    Hi there. Thanks for you post. Have you figured out how to get it to dispose of the MapPoint instance. I am ending up with an instance open for every call.

    • Matt says:

      //Kill the MapPoint instance at the end of the drive time method:

      foreach (System.Diagnostics.Process killMe in System.Diagnostics.Process.GetProcesses())
      {
      if (killMe.ProcessName == “MapPoint”)
      {
      killMe.Kill();
      }
      }

  5. Matt says:

    Neat stuff – I’ve been looking for a cost effective drivetime polygon solution. Do you know if same drivetime polygon functionality can be found in the Bing API, as Microsoft is now retiring MapPoint?

    • alastaira says:

      Microsoft isn’t retiring *MapPoint* – it’s retiring the *MapPoint Web Services* (which are being replaced by the Bing Maps Web Services).
      You can’t do drivetime polygons in Bing Maps API, sadly, only in the desktop MapPoint product as far as I know.

  6. Hello, i am a new user of Mappoint 2013 and i would like to know if it´s possible to extract, from a drivetime area, all the postalcodes inside it, onto an excel file.
    Thanks

Leave a comment