Find method calls (both FindPlaceResults and FindAddressResults) in MapPoint returns the FindResults instance that contains all the locations that matched the input query. Each location that matched the input place is represented as an instance of MapPoint.Location class in the FindResults object. Even though the FindResults class is essentially a collection of Location objects, its behavior is quite different (blame the COM legacy) from any .NET collection in that you can’t really access the members of the collection with their index as you would in a generic .NET collection.

Having said that you can still access the collection members in the following ways:

Using the Get accessor method

Collections are implemented using two special accessor methods called get_Item and set_Item. These special methods are generated by the .NET compiler for every publicly exposed property. In case of FindResults instance, even though the Items read-only property is not exposed in the interoperable assembly, get_Item method is still implemented internally. The only differences from the conventional .NET get_Item method are that this method takes the index value as an object passed by reference (for COM marshalling purposes) and the index starts from 1 instead of 0.
The following code shows how to use the get_Item accessor method to iterate through the list of Location objects in the FindResults collection:

//Create an object to hold index value
object index = null;
//Start with index 1 instead of 0
for(int i=1; i<=findResults.Count; i++) { //Box the integer value as an object index = i; //Pass the index as a reference to the get_Item accessor method MapPoint.Location loc = findResults.get_Item(ref index) as MapPoint.Location; if(loc != null) //do something intersting }


Using an enumerator

You can also access the list of Locations from FindResults instance using the enumeration. The FindResults instance exposes a method, GetEnumerator, to get an enumerated list of locations. Using this enumerator instance, you can loop through the list of locations as shown below:

//Get an enumerator using the GetEnumerator method
IEnumerator locationEnumerator = findResults.GetEnumerator();
//Loop through the location instances to get the names
while(locationEnumerator.MoveNext())
{
MapPoint.Location loc = locationEnumerator.Current as MapPoint.Location;
//do something interesting
}

To learn more about this topic and other related topics, please buy Programming MapPoint in .NET

0 comments