private static BingGeocodeService.Confidence TranslateGeocodeConfidence(GeocodeConfidence confidence) { BingGeocodeService.Confidence result = BingGeocodeService.Confidence.Low; switch (confidence) { case GeocodeConfidence.High: result = BingGeocodeService.Confidence.High; break; case GeocodeConfidence.Medium: result = BingGeocodeService.Confidence.Medium; break; } return(result); }
private static GeocodeConfidence TranslateBingConfidence(BingGeocodeService.Confidence confidence) { GeocodeConfidence result = GeocodeConfidence.Zero; switch (confidence) { case BingGeocodeService.Confidence.High: result = GeocodeConfidence.High; break; case BingGeocodeService.Confidence.Medium: result = GeocodeConfidence.Medium; break; case BingGeocodeService.Confidence.Low: result = GeocodeConfidence.Low; break; } return(result); }
// Geocodes the given address and returns the confidence level public GeocodeConfidence GeocodeAddress(string address, string city, string state, string zip, out double lat, out double lon, GeocodeConfidence minConfidence) { if (string.IsNullOrEmpty(address)) { throw new ArgumentException("Missing 'address' parameter."); } if (string.IsNullOrEmpty(city)) { throw new ArgumentException("Missing 'city' parameter."); } if (string.IsNullOrEmpty(state)) { throw new ArgumentException("Missing 'state' parameter."); } if (string.IsNullOrEmpty(zip)) { throw new ArgumentException("Missing 'zip' parameter."); } // Reference geocode client BingGeocodeService.GeocodeService client = new BingGeocodeService.GeocodeService(); // Set the options to only return high confidence results BingGeocodeService.ConfidenceFilter[] filters = new BingGeocodeService.ConfidenceFilter[1]; filters[0] = new BingGeocodeService.ConfidenceFilter(); filters[0].MinimumConfidence = TranslateGeocodeConfidence(minConfidence); // Build geocode request BingGeocodeService.GeocodeRequest request = new BingGeocodeService.GeocodeRequest(); request.Query = address + ", " + city + ", " + state + ", " + zip; request.Credentials = _geoCredentials; // Issue request BingGeocodeService.GeocodeResponse response = client.Geocode(request); // Set out params if (response.Results.Length > 0) { lat = response.Results[0].Locations[0].Latitude; lon = response.Results[0].Locations[0].Longitude; return(TranslateBingConfidence(response.Results[0].Confidence)); } else { lat = 0; lon = 0; return(0); // no confidence, bad result } }