A geocoding service would be the primary way to do this, however there some things to consider.
- Geocoders are primarily designed to take an address and get the coordinates, but as the saying goes "garbage in, garbage out". The geocoders in Bing Maps and Azure Maps have some pretty good parsing and correcting logic in there and handles spelling and address string formatting issues pretty well, so this may meet your needs depending on how "rough" your input is.
- Geocoders are generally not strict on house/building numbers. If you pass in a house number that doesn't exist, geocoders won't say it's wrong but will generally interpolate the position on the section of road based on the known address ranges of a road segment. For example, on a section of road that has numbers from 1 to 100, if you pass in an address, it house number 50 it may assume the middle that address is located at the center of that road segment, even if no house with that number exists. It's also worth noting that unit numbers (e.g. apartment numbers) are generally stripped away and not included in the response. The response from the Bing Maps and Azure Maps geocoders do include the matched address information. Assuming this is the correct address you are looking for, spelling errors and most formatting issues will be resolved. In some cases, it's also possible to get the address details in different languages.
Bing Maps
If using Bing Maps, the Bing Maps geocoding service with a couple of different APIs:
- Free form address string query API - Pass in a single line string address and this API will parse and process it. This is generally the most reliable address geocoding API in Bing Maps.
- Structured address queries API - Pass in the individual address parts (street address, city, state...) as individual parameters.
- Batch geocoding API - Not really for your scenario but worth mentioning for completion. This API can process 200,000 addresses in a single long running job (could take hours).
To use the Bing Maps REST APIs in .NET there is an open source library called BingMapsRESTToolkit. Here is a simple example of how to use this library:
//Create a request.
var request = new GeocodeRequest()
{
Query = "New York, NY",
BingMapsKey = "YOUR_BING_MAPS_KEY"
};
//Process the request by using the ServiceManager.
var response = await request.Execute();
if(response != null &&
response.ResourceSets != null &&
response.ResourceSets.Length > 0 &&
response.ResourceSets[0].Resources != null &&
response.ResourceSets[0].Resources.Length > 0)
{
var result = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;
//Do something with the result.
}
Additional documentation on using the Bing Maps REST APIs in .NET can be found here. I would only bother with this if you want to skip using this above mentioned library to minimize app size and easily deserialize the Bing Maps response directly.
Azure Maps
If you are building an app that will run in Azure you may want to consider using Azure Maps since it simplifies the licensing and is natively available in Azure. Azure Maps has several different types of geocoding and search API's available. For addresses you will want to use the V2 geocoding API as it is the most accurate and handles "dirty" input better. Under the hood this is the same geocoder that's in Bing Maps so results should be the same, but with a more modern response (the responses use the open-standard GeoJSON objects rather than a custom Bing Maps specific response object). Also worth noting that Azure Maps provides enhanced security options; such as Azure Active Directory support.
The Azure Maps team just released a .NET library a couple of months ago with [full documentation on how to use it in a .NET app here]. However, it doesn't yet include support for the V2 geocoding service. That said, since the response is an open standard, you can easily use any GeoJSON reading library in .NET to parse the response. Here is a quick example that uses the GeoJSON.NET library.
//Query and Azure Maps subscription key.
var query = "1 microsoft way, Redmond, WA";
var subscriptionKey = "<Your Azure Maps Key>";
//Create the request URI.
var requestUri = $"https://atlas.microsoft.com/geocode?api-version=2022-09-01-preview&query={query}&subscription-key={subscriptionKey}";
//Make the request.
var client = new HttpClient();
var responseString = await client.GetStringAsync(requestUri);
//Deserialize the response into a GeoJSON object.
var response = JsonConvert.DeserializeObject<FeatureCollection>(responseString);
if (response.Features.Count > 0)
{
//Get the first result.
var firstResult = response.Features[0];
//Get the clean address string.
var address = firstResult.Properties["address"] as dynamic;
var cleanAddressString = address.formattedAddress;
//Get latitude and longitude information.
var point = firstResult.Geometry as GeoJSON.Net.Geometry.Point;
var latitude = point.Coordinates.Latitude;
var longitude = point.Coordinates.Longitude;
//Do something with this data.
}