Search code examples
c#.netmappoint

MapPoint 2011 FindAddress dialog in .NET


I am adding a list of addresses into Mappoint using C#.

foreach (Stop stop in _stops)
                _route.Waypoints.Add(_mpMap.FindAddressResults(stop.Street, stop.City, "", "Oregon", stop.Zip)[1]);

Sometimes address format is wrong and because of that I get either crash or complected wrong address.

In mappoint (application) you can search for places and if mappoint finds multiple or you make a mistake in address, it opens a find and gives you options to search and/or add address anyway.

Example: enter image description here

Notice how entered address is poorly formatted, but mapoint could easly find full address with normal formatting. Sometimes there are more results and I need to be able to select manually if that happens. Question: How?

Added later on:

I can call dialog itself with method ShowFindDialog and I can get the count of results found with .Count parameter

MapPoint.FindResults results = _mpMap.FindAddressResults(stop.Street, stop.City, "", "Oregon", stop.Zip);
MessageBox.Show("Found " + results.Count + " results");

But I can't find a way to specify address to ShowFindDialog


Solution

  • You are abusing FindAddressResults. This does not return a simple array (which is how you are treating it), but a FindResults collection. The FindResults collection includes a property called "ResultsQuality". This is fully documented in the help file that comes with MapPoint, but you must check this value before blindly assuming the collection contains one or more results!

    The ResultsQuality property is set to a GeoFindResultsQuality enumeration. You want to check for geoAllResultsValid (0) or geoFirstResultGood (1). The other values indicate no results or ambiguus results.

    Here's the VB6 example from the docs:

    Sub AddPushpinToGoodFindMatch()
    
    Dim objApp As New MapPoint.Application
    Dim objFR As MapPoint.FindResults
    
    'Set up the application
    objApp.Visible = True
    objApp.UserControl = True
    
    'Get a FindResults collection
    Set objFR = objApp.ActiveMap.FindResults("Seattle")
    
    'If the first result is a good match, then use it
    If objFR.ResultsQuality = geoFirstResultGood Then
        objApp.ActiveMap.AddPushpin objFR.Item(1)
    Else
        MsgBox "The first result was not a good match."
    End If
    
    End Sub
    

    FindResults() is an old method which returns the same FindResults class, but using FindAddressResults (as you are doing) is generally a much better thing to do.


    Addenda: As this general problem is such a common problem (probably due to bad sample code in the MapPoint docs that is blindly cut&pasted), I've written an article about using the FindResults collection correctly, on my "MapPoint HowTo" pages.