/// <summary> /// Find a location like an address (1500 Central rd., Chicago, IL) or landmark (Sears tower or Navy Pier) /// </summary> /// <param name="locationString">address or landmark</param> /// <returns>Location</returns> public Location FindLocation(string locationString) { Location[] location = null; try { if (locationString == "") { throw new System.ArgumentNullException("Location cannot be empty"); } FindSpecification myFindSpec = new FindSpecification(); myFindSpec.InputPlace = locationString; myFindSpec.DataSourceName = "MapPoint.NA"; FindResults results = theMapPointFindService.Find(myFindSpec); // if there is no result found try it as an address instead if (results.NumberFound == 0) { // if you want to use addresses instead you can use the code below Address address = theMapPointFindService.ParseAddress(locationString, "USA"); FindAddressSpecification myFindASpec = new FindAddressSpecification(); myFindASpec.DataSourceName = "MapPoint.NA"; myFindASpec.InputAddress = address; results = theMapPointFindService.FindAddress(myFindASpec); } // at this point a place (e.g. Sears Tower) or an address was not found so // return an error if (results.NumberFound == 0) { throw new System.ArgumentNullException("Location cannot be found"); } return(results.Results[0].FoundLocation); } catch (ArgumentNullException e) { throw e; // rethrow for app to handle } catch (Exception e) { throw e; // rethrow for app to handle } }
private static GeoPoint GetGeoPositionMicrosoft(string address, string plz, string city, string region, string country) { GeoPoint geoPoint = null; FindServiceSoap findService = new FindServiceSoap(); string myUserID = Properties.Settings.Default.MPUser; string myPassword = Properties.Settings.Default.MPPass; NetworkCredential myCredentials = new NetworkCredential(myUserID, myPassword); findService.Credentials = myCredentials; findService.PreAuthenticate = true; string addressLine = (address != null) ? address.Trim() : string.Empty; string zipCode = (plz != null) ? plz.Trim() : string.Empty; string stadt = (city != null) ? city.Trim() : string.Empty; string kanton = (region != null) ? region.Trim() : string.Empty; string land = (country != null) ? country.Trim() : string.Empty; string dataSourceName = "MapPoint.EU"; if (land.Length == 0) { land = "switzerland"; } if (kanton.Length == 0) { kanton = stadt; } FindOptions myFindOptions = new FindOptions(); myFindOptions.ThresholdScore = 0.5; myFindOptions.Range = new FindRange(); myFindOptions.Range.StartIndex = 0; myFindOptions.Range.Count = 1; //Try find exact address Address myAddress = new Address(); myAddress.AddressLine = addressLine; myAddress.PrimaryCity = stadt; myAddress.SecondaryCity = string.Empty; myAddress.Subdivision = kanton; myAddress.PostalCode = zipCode; myAddress.CountryRegion = land; FindAddressSpecification findAddressSpec = new FindAddressSpecification(); findAddressSpec.InputAddress = myAddress; findAddressSpec.Options = myFindOptions; FindSpecification findSpec = new FindSpecification(); string inputPlace = string.Format("{0},{1},{2}", city, kanton, land); inputPlace = inputPlace.Replace(",,", "").TrimStart(','); findSpec.InputPlace = inputPlace; FindResults myFindResults = null; FindResult[] myResults = null; try { findAddressSpec.DataSourceName = dataSourceName; myFindResults = findService.FindAddress(findAddressSpec); } catch { } if (myFindResults != null && myFindResults.Results != null && myFindResults.Results.Count() > 0) { myResults = myFindResults.Results; if (myResults.Count() > 0) { geoPoint = FillGeoPoint(myResults[0]); } } else { //Try finding by Location try { findSpec.DataSourceName = dataSourceName; myFindResults = findService.Find(findSpec); } catch { } if (myFindResults != null && myFindResults.Results != null && myFindResults.Results.Count() > 0) { myResults = myFindResults.Results; geoPoint = FillGeoPoint(myResults[0]); } } if (geoPoint == null) { //still nothing //try it again with new datasource myFindResults = null; myResults = null; dataSourceName = "MapPoint.NA"; try { findAddressSpec.DataSourceName = dataSourceName; myFindResults = findService.FindAddress(findAddressSpec); } catch { } if (myFindResults != null && myFindResults.Results != null && myFindResults.Results.Count() > 0) { myResults = myFindResults.Results; if (myResults.Count() > 0) { } } else { //Try finding by Location try { findSpec.DataSourceName = dataSourceName; myFindResults = findService.Find(findSpec); } catch { } if (myFindResults != null && myFindResults.Results != null && myFindResults.Results.Count() > 0) { myResults = myFindResults.Results; geoPoint = FillGeoPoint(myResults[0]); } } } return(geoPoint); }