Finding Locations with the MapPoint Web Service Find APIs
(Page 1 of 4 )
In this third part to a five-part series on the Find Service, you'll learn how to find addresses, points of interest around a location, and more. It is excerpted from chapter six of the book
Programming MapPoint in .NET, written by Chandu Thota (O'Reilly; ISBN: 0596009062). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Selectively finding entity types
Notice that this list includes all kinds of entities, such as city halls, parks, libraries, and schools in the result list. Imagine for now that you need only a list of cities named after Redmond—you need to tell MapPoint Web Service that you are only looking for city entity matches to your query. You can do this using the FindSpecification.EntityTypeNames field. The EntityTypeNames field is an array of strings that represents the entity type names that the Find method needs to look for. Since you are only interested in cities named Redmond, pass the entity type name PopulatedPlace :
//Assign entities to search
fndspec.EntityTypeNames = new string[] {"PopulatedPlace"};
A call to the Find method with this addition returns the following nine cities named Redmond:
Redmond, Washington, United States
Redmond, Oregon, United States
Redmond, Western Australia, Australia
Redmond, Larimer, Colorado, United States
Redmond, Butler, Pennsylvania, United States
Redmond, Sevier, Utah, United States
Redmond, Mason, West Virginia, United States
Redmond Corner, Oneida, New York, United States
Redmondville, Iron, Missouri, United States
Even though your threshold score is 0 and there are 500 returned results requested, by assigning specific entity type, the find is narrowed down to 9 results from the original 32 results.
Limiting search to a geographic area
Notice that the above list contains cities from both the United States and Australia. If you are looking only for cities in the United States and need to instruct MapPoint Web Service to limit the search within a geographic boundary, using the FindOptions.SearchContext field you can limit the search to a particular geographic area. The SearchContext field is an integer value that represents the entity ID of a spe cific geographic area. Since you are specifically looking for cities named Redmond in the United States, the context ID should be set to the United States country entity ID 244 . The following code shows the addition of search context to the find request:
//Assign country context for United
State s
findspec.Options.SearchContext = 244;
With this addition, the search now only returns the following 8 results:
Redmond, Washington, United States
Redmond, Oregon, United States
Redmond, Larimer, Colorado, United States
Redmond, Butler, Pennsylvania, United States
Redmond, Sevier, Utah, United States
Redmond, Mason, West Virginia, United States
Redmond Corner, Oneida, New York, United States
Redmondville, Iron, Missouri, United States
The list now includes only cities in the United States.
Finding geographic entities with no input place name
The Find method is very powerful because it allows you to find geographic entities without actually specifying a place name. Example queries include: “Find all states in the United States” and “Find all airports in Australia.” You can perform these queries based on entity type names, geographic contexts, or by assigning the input place name a null value. The following code shows how to get all state names in the United States:
//Create find service soa p
FindServiceSoap findsoap = new FindServiceSoap();
//Assign credentials
. . .
//Create FindSpecification
FindSpecification findspec = new FindSpecification();
//Assign data source
findspec.DataSourceName = "MapPoint.NA";
//Assign null to input place
findspec.InputPlace = null;
//Create find options
findspec.Options = new FindOptions();
//Set result count
findspec.Options.Range = new FindRange();
//Set to the maximum count
findspec.Options.Range.Count = 500;
//Set threshold score to zero
findspec.Options.ThresholdScore = 0;
//Assign state entity type to search
fndspec.EntityTypeNames = new string[] {"AdminDivision1"};
//Now call find
FindResults findresults = findsoap.Find(findspec);
//Assign found count
foreach(FindResult findresult in findresults.Results)
{
//Display results
. . .
}
This search results in 51 entities (50 states and Washington D.C.).
To explore more on data sources, entity types, and entity based finds, I have included an application, MapPoint Web Service Data Source Browser, on the com panion material as part of the Chapter06 sample solution. Figure 6-2 shows a screen-shot of the application with results for the query “Find all airports in Australia.”
Finally, it is important remember that, due to performance reasons, for any find query, the maximum number of results returned ( FindResult objects) is 500; you cannot issue a find query such as “Find all cities in the world,” but if you do have such a requirement, I recommend breaking down the query to get a manageable result set that is less than or equal to 500 each time. An example of such implementation would be to provide a browse functionality where your customers can select a country first, a state second, a county third, and then find all cities within that county without hitting any maximum result count issues, since you are confining your query to a smaller, limited geographic area.
Next: Finding Addresses >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter six of the book Programming MapPoint in .NET, written by Chandu Thota (O'Reilly; ISBN: 0596009062). Check it out today at your favorite bookstore. Buy this book now.
|
|