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

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
SVG Tutorials
MapPoint

Map Visitors

  ARTICLES  


Using MapPoint 2001 from C++ - the 'FindAddress' Problem

The MFC ClassWizard makes using MapPoint from C++ almost as easy as it is from VB. This article explains the basics, and provides a plug-in replacement for the MapPoint 2001 "FindAddress" not supported by ClassWizard

The simplest way talk to MapPoint via automation from C++/MFC is using ClassWizard generated wrappers - here's a brief recap how this is done:

  • create or open an MFC-based application

  • open ClassWizard e.g. by pressing Ctrl-W

  • press the "Add Class ..." button and select "from a type library"

  • locate the type library for your version of MapPoint - look for a file with a .tlb extension in the MapPoint installation directory. For the North American version of MapPoint 2001, it is called MPNA80.TLB. Select this file and press Open.
    [NOTE: If you have MapPoint 2002, the corresponding type library is called MPNA81.TLB or MPEU81.TLB in the European version.]

  • For each class of the MapPoint object model you want to use in your project (e.g. _Application, _Map, Location, Pushpin), do the following:
    • select the class
      [NOTE: If you have MapPoint 2002 installed but would like to stay compatible with MapPoint 2001 on your users machines, select the classes that have an "80" in their name]

    • rename it under "class name" - use a consistent prefix, e.g. prefix all your MapPoint objects with "Mp"

  • If you want, you can also rename the .CPP and header file that will be generated.

  • Make sure all classes you want included are selected.

  • Press OK, then OK again.

  • Make sure you initialize the COM library by adding a call to AfxOleInit() in the InitInstance method of your app.

  • don't forget to #include the generated header file for the MapPoint wrapper objects in your source code - the default name e.g. for MapPoint 2001 North America is MPNA80.H

After you have completed these steps, you can check whether MapPoint obeys your commands by adding e.g. the following code to the event procedure of a button or menu item:

   MpApplication mpApp;

   mpApp.CreateDispatch("MapPoint.Application");

   mpApp.SetVisible(true);

   MessageBox("MapPoint should be visible now ...");

   mpApp.Quit();
   mpApp.ReleaseDispatch();
One problem with the wrapper classes generated for MapPoint 2001 by the class wizard is that due to an MFC limitation, the FindAddress method of the Map class cannot be generated - instead, you will find the following comment in the generated code:
  // method 'FindAddress' not emitted because of invalid 
  // return type or parameter type
The problem is that "FindAddress" uses a variable-length list of arguments, and the COleDispatchDriver MFC class used by the wizard as base class for your wrapper classes and responsible for packaging and invoking automation calls, does not understand how to package the SAFEARRAY type.

A detailed description of the problem can be found in Microsoft knowledgebase article Q158451.

The good news is that a replacement "FindAddress" can be added relatively easily - the corresponding code is shown below.

This routine sets up the arguments and calls the Invoke method of the COleDispatchDriver class itself. On input, it accepts a (mandatory) street address, additional (optional) address strings, and finally, a country code that indicates which country the address should be searched in.

(The country codes can be determined e.g. by opening the MapPoint type library in the OLE/COM object viewer available on the VC++ Tools menu)

To provide the map wrapper object the wizard generated for you with a FindAddress method, add the code above to the corresponding implementation file (e.g. MPNA80.CPP). Make sure you adapt the object name to what you selected at creation time - if your map object wrapper is called "MaptMap" instead of "MpMap" as in my case, change the MpMap::FindAddress above accordingly. Also, add the following declaration:

LPDISPATCH MpMap::FindAddress(LPCTSTR szStreet, 
                  const CStringArray& sarAddressStrings, 
                  short nCountryCode);
to the wrapper header file (e.g. MPNA80.H), e.g. after below the "FindAddress not emitted" comment inserted by the wizard. Here's a short snippet that shows how you would use the routine.

   MpApplication mpApp;
   MpMap mpMap;

   mpApp.CreateDispatch("MapPoint.Application");
   mpApp.SetVisible(-1);

   mpMap = mpApp.GetActiveMap();
   
   CStringArray sarAddress;

   sarAddress.Add("Redmond");
   sarAddress.Add("WA");
   sarAddress.Add("98052");

   MpLocation mpLoc = mpMap.FindAddress(
                            "1 Microsoft Way", 
                            sarAddress, 
                            244);
   mpLoc.GoTo();

   MessageBox("Press OK to close MapPoint");

   mpLoc.ReleaseDispatch();

   mpMap.SetSaved(-1);
   mpMap.ReleaseDispatch();
   
   mpApp.Quit();
   mpApp.ReleaseDispatch();
The snippet creates a MapPoint application object, makes it visible, and retrieves the active map. It then adds the address components (except for the street which is always passed as the first parameter) to a CStringArray. The location returned by the FindAddress method of the map object is stored in a corresponding object variable which is used to center the map around the address.

Conclusion: With ClassWizards help, remote-controlling MapPoint and other automation servers is a breeze. Methods with variable argument lists need some extra attention, but the problem is solvable.

In MapPoint 2002, "FindAddress" is still available for compatibility reasons, but it has been replaced by the more powerful "FindAddressResults" method which can return several addresses at once, and is supported by ClassWizard.

Discuss this story in the forum.

Author: Gilles Kohl
Email: gilles(AT)_deletethisincludingunderlines_compuserve.com
URL: http://www.procad.de
Gilles Kohl, a native of Luxembourg living in Germany, is a software development lead with PROCAD GmbH of Karlsruhe, Germany.

Mapping and especially GPS-related topics are a hobby - Gilles enjoys developing solutions for Microsoft MapPoint and his favorite outdoor occupation is confluence hunting.

As always, please direct questions to the newsgroup.



Google
 
MP2Kmag Internet


 Recent Discussion
-GCPowerStation v17.2
-GCPowerStation v17.2
-Gaussian 09W v8.0 Rev B.01 SMP
-Gaussian 09 E.01 Linux64
-Atmel Studio v.7.0.1931
-Steinberg Cubase Elements v9.5.40 WiN
-Nero Platinum 2019 Suite 20.0.05900 Multilingual
-Serif DrawPlus X8 14.0.0.19
-PROMT Master 19 Final + PROMT 19 Dictionary Collection
-Apple Logic Pro X v.10.4.2 Multilanguage (MacOSX)
-PROMT Expert 19 Final + PROMT 19 Dictionary Collection
-Waves Complete v2018.10.16 WiN by R2R
-VMware Fusion Pro 10.1.1 MacOSX
-Foxit Phantompdf Business v.9.3.0.10826 Multilanguage
-Bluestacks v4.31.56.2510 Multilingual
-Xara Web Designer Premium 16.0.0.55162 (x64) Portable
-Adobe Bridge CC 6.1.1.10 Update 3 by m0nkrus
-Garmin MapSource v5.4
-Alien Skin Exposure X4 Bundle 4.0.2.43 | macOS
-Unity Asset Cubebrush - Scrap City Assets 1.0 x64
-Magix Samplitude Pro X3 Suite v.14.4.0.518 Multilanguage
-Waves Complete v2018.10.16
-Oxygen XML Editor 18.0 (Win/Mac/Lnx)
-Steinberg Cubase Elements v9.5.40 WiN
-The Foundry Nuke Studio 11.2v4 Win x64
 Resources
Browse GIS books and periodicals
Find a MapPoint Partner or Consultant
Real Estate Columbia MO Homes for Sale


Want Your Site To Appear Here?

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