spacer1
spacer2 1_1 1_2
2_1
 Subscribe
 The MP2K Update!
 
 
 
 Magazine
Front Cover
What's New
Articles
Toolbar
News
Sample Data
Gallery
Advertise
About
 Features
MapPoint 2006
Press Releases
MapPoint Forums
Companies
Link to MP2Kmag
Wish List
MapPoint Trial
Authors
 Sponsors
 Order

MapPoint 2006

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
ViaVirtualEarth
SVG Tutorials
MapPoint

Map Visitors

 Direct Media

IT SUPPORT LONDON
Professional, flexible and highly skilled IT support - On call and on site. Go to Connect.co.uk.

inks
See our comprehensive range of inks. See our amazing prices. See for yourself. Great value, great range and great customer service.

Cheap flights to Goa
Check out Holiday Hypermarket to book your cheap flights to Goa. A unique and exciting destination. Get set to jet off!

Cheap Holidays in Egypt
Check out the cheap holidays in Egypt with On The Beach. Our low price promise leaves other holiday operators in the dust! See for yourself.

apartments Bulgaria
Apartments available for sale in ski resorts in an around Bulgaria. Choice of custom build or ready made!

Cheap Holidays to Fuerteventura
Searching for cheap holidays to Fuerteventura? Let dealchecker.co.uk make it easier. Use our simple holiday search to bag a bargain holiday.

Cheap Portugal Holidays
Want to know what the best time to book cheap Portugal holidays is? Well have a look on ulookubook.com for some useful information to save your cash. Golf, tennis and cycling are all very popular here.

  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
-Virtual Earth on CSI: New York
-Helping out after the China quakes
-Coastal Boundaries
-Mapping Earthquakes in China
-An invalid argument was encountered.
-Navman Wireless Oem Solutions Enhances Leading Line Of Gps Receiver Modules
-T-mobile UK
-Virtual Earth and OpenLayers
-Reminder: Microsoft Virtual Earth Partner Webinar and Offer for Free Training
-Virtual Earth Quarterly Technical Briefing
-Customize User Page
-Information about area
-Territory information
-Gps task pane in ActiveX
-Worldwide Telescope Now Available!
-c++ - setting the color of a line
-Initial Costa Rica MapServer map is up & running!
-Satellite Images of Myanmar Cyclone Damage
-Virtual Earth Loyalist: Aerial View
-Leaving messages on the Virtual Earth for Government blog site
-Virtual Earth Webcast: Adding Mapping Capabilities to your Applications with Virtual Earth
-Get new Lat & Lon from speed
-Integrating Virtual Earth Maps and Excel 2007
-Zillow.com
-ADODB Recordset to MapPoint.DataSet
 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-2008 MP2K. Questions and comments to: website@mp2kmag.com
  Microsoft and MapPoint 2002/2004/2006 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, Ink Cartridges & Drums at Marquee Office Solutions
• Looking for Dry Erase Boards? See Dry Erase Boards
• 5339 - We Know Bikes: Road Bikes / Triathlon Bikes / Mountain Bikes / BMX Bikes
• Buy Your Car Used Cars, Car Lease and Contract Hire