Using StreetMapData for GeoCoding with ArcGIS Mobile
The StreetMapData contains street networks that can be used for geocoding on a mobile device. The locators exposed by the Streetmapdata can be used to return a number of results, which are refined through a series of filters to narrow the result set.
// Get the StreetName locator by City and State Locator streetLocator = _streetMapLayer.StreetMapDataset.Locators[0]; streetLocator.Reset(); // Get the first filter LocatorFilter _stateFilter = streetLocator.GetNextFilter(); _stateFilter.AppendToFilter(txtState.Text.Trim()); // Gets the city filter LocatorFilter _cityFilter = streetLocator.GetNextFilter(); _cityFilter.AppendToFilter(txtCity.Text.Trim()); // Gets the street filter LocatorFilter _streetFilter = streetLocator.GetNextFilter(); _streetFilter.AppendToFilter(txtStreet.Text.Trim()); // Gets the housenumber filter LocatorFilter _houseNumberFilter = streetLocator.GetNextFilter(); _houseNumberFilter.RemoveAllCharacters(); _houseNumberFilter.AppendToFilter(txtStreetNum.Text.Trim()); // get the result AnalyzeGeoCodingResults(_houseNumberFilter);
Once the filters have been created and assigned to the Locator the next step is to get the results. As multiple results maybe returned from the Locator, normally all the results are provided to the user so they can chose the desired one. The Locator may return no results, so it is important to check for this condition.
if(filter.GetResults().Count() <= 0) { MessageBox.Show("Cannot find this address !"); return; } // only get the first one LocatorResult result = filter.GetResults().First(); //foreach (LocatorResult result in filter.GetResults()) { Location candidate = result.GetLocationCandidates().First(); //foreach (LocatorCandidate candidate in result.GetCandidates()) { // if the map control has a mobile service, need to do the conversion. // coordinate = mobileService1.SpatialReference.FromWgs84(lon, lat); ptLocated = new ESRI.ArcGIS.Mobile.Geometries.Point(candidate.Coordinate); } } }
Where to get the sample
The sample is available for download from here.
9/20/2011