Follow us on Twitter

Calling MapPoint from C++ without MFC

Share:

If you wish to use MapPoint’s COM interface from C++, then you will quickly find that documentation and tutorials are virtually non-existent. The few examples that do exist use a handful of COM functions and classes from MFC (Microsoft Foundation Classes); but many Win32 C++ programmers use non-Microsoft compilers and development environments. Also, many of those who do use such environments are moving away from MFC.

MFC was great when it appeared about ten years ago, but now it is a proverbial dinosaur: old and lumbering. Standard libraries such as the STL have superseded much of the MFC. For a while now, MFC has only really been necessary for its object oriented GUI interface. With the advent of the much smaller WTL (Windows Template Library), we can finally say goodbye to MFC.

The following tutorial shows you how to use C++ to control MapPoint through its COM interface whilst avoiding MFC. The code has been built and executed using Visual C++ 5.0, and has also been tested against the IA-32 version of the Intel 7.1 compiler. It should be fairly simple to use this code with other Windows C++ environments and compilers.

If you are going to use the WTL, you will need to install it. WTL is available as a
free download from Microsoft. Tutorials and guides for using the WTL can be found at CodeGuru.

So let’s get started!

First create a new project. If you have the WTL installed, use the ATL/WTL AppWizard. For this tutorial I chose an SDI Application, but you can choose any of the application templates. I find Dialog Applications are very useful for utilities. Select the COM Server option if you wish to receive events or if you wish to create an Add-in. Your application does not need to be a COM server if it is only going to receive objects from MapPoint.

If you are not using the WTL AppWizard, you will need to initialize the COM system. WTL does this in the function _tWinMain, with the line:

Then at the end of execution, finishes with:

These must be in the same thread as your MapPoint calls.

You now have an application that can use COM objects. Next you need to tell your program about the MapPoint API. To do this, you need to locate MapPoint’s .tlb file, which can be found in your main MapPoint directory. For MapPoint 2004 North America, the file is called “mpna82.tlb“.

Create a source file called “mpinterfaces.cpp” and include the following, substituting the .tlb pathname:

 

Similarly, create an include file called “mpinterfaces.h” and include the following:

Note the “#undef EOF“: This is a bit of a kludge to get around some poor design on Microsoft’s part. The MapPoint interface includes a definition for "EOF“, even though this is already defined in various standard
C++ include files! The #undef helps to fix this, but it is strongly recommended that you do not mix file I/O (eg. steams) and MapPoint calls in the same class definition.

Add these two files to your project. You can now perform a test build. If you run your application, you should get an empty template application with no errors.

Now we can add some calls to control MapPoint. For this tutorial, I shall add the MapPoint code to the OnFileNewMethod of CMainFrame (in mainfrm.h, created by the wizard). This is for demonstration purposes only; in reality you would put it somewhere else.

First, add the following two lines to the beginning of mainfrm.h:

The tutorial code for OnFileNewMethod is:

Hopefully, most of this code is fairly self-explanatory, although many of the objects and methods might be new. I shall go through it line by line.

The main MapPoint application object is called _Application, and is encapsulated by _ApplicationPtr. The encapsulation handles things like reference counting and passing between application threads. Whenever MapPoint returns (or creates) a new object it returns it encapsulated like this.

The code below will create an instance of MapPoint using the default version. As with Visual Basic, you can select different versions, e.g. “MapPoint.Application.NA.11”.

To do anything useful, we need to get a valid pointer to the _Application object.

myMapPoint is a pointer to our _Application object. Members and methods are called as with conventional C++. For example, to display MapPoint:

Let’s create some other objects. _Map contains most of the functionality, so we’ll get a pointer to that first:

Now we can find some locations. To create a Location object for a known longitude and latitude, we use the _Map.GetLocation() method:

Adding a pushpin is just as easy:

Once we have a Location we can move to it, using the Location‘s Goto() method:

Locations can also be searched for. This is performed by using the _Map.FindAddressResults() method, which returns a FindResults object:

The FindResults object is actually a collection. This is accessed as a 1-referenced array, using the GetItem() method to access the individual locations.

To pass values to GetItem() we must use the VARIANT class. There is probably a more convenient template, but the following works well:

VARIANTs are used to pass objects of different types. This VARIANT is set up to be a 4 byte integer, and is used to pass the value ‘1’ to myResults->GetItem(). Here it is a very verbose way of performing things, but VARIANTs implement a lot of flexibility and can be used to pass different types including strings and lists of locations.

Now we have a Location object for Hot Springs, we can add a pushpin there as well:

We have two locations, so lets find a route between them both. MapPoint only supports one route at a time. A reference to this is accessed with the _Map.GetActiveRoute() method.

Waypoints are also implemented as collections, but adding a waypoint to the end of the current collection is easily performed using the Add() method:

The route is calculated using the Route.Calculate() method. Calculate() will throw an exception if the wrong version of MapPoint (Europe vs. North America) is being used, due to the lack of roads for suitable routes.

That is it! Before we stop and wait for the user, we should tidy some things up. We have made temporary changes to the map, so it would be good to suppress the “Save Map?” dialog box when MapPoint exits. Do this with the
_Map.Saved member:

Next we must release all of the COM objects. When MapPoint finally exits, it will remain in memory until all references to all MapPoint COM objects are removed.

Next we display a simple modal box and wait for the user to click “OK”:

Next we close MapPoint with the Quit() method. Note that this will only work if the user has not already closed MapPoint. If they have done, then Quit() will throw an exception, and a zombie MapPoint will stay in memory. The best way to get around this in a production system is probably to trap the Quit event from MapPoint.

So there you have it, a simple application in C++ without any of the MFC overhead! This approach works well for me and I have already used it in a number of applications. It should also work in other C++ development environments with only a few minor changes.

Addenda

If you are working with C++ Builder, you do not need to create the mpinterface.h and mpinterface.cpp. Instead,
you should select the “Import Type Library” menu selection, and then drag & drop the MapPoint OLE Components TApp and TMap into the CBuilder form. C++ Builder does not bring in all of the object properties, so sometimes you have to use the set and get methods instead. Eg.

1 comment to Calling MapPoint from C++ without MFC

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">