Skip to content

Instantly share code, notes, and snippets.

@liketaurus
Created October 26, 2017 16:01
Show Gist options
  • Save liketaurus/9826da022c50915cac9267739b8ed744 to your computer and use it in GitHub Desktop.
Save liketaurus/9826da022c50915cac9267739b8ed744 to your computer and use it in GitHub Desktop.
How to use Bing Maps geocode REST services
// Legacy Bing geocoding SOAP services were recent shut down after being replaced by the REST services
// so, you need to migrate your app to use the REST services.
//
// If your application uses .NET you will likely find this library useful:
// 'Bing Maps REST Services Toolkit' - https://github.com/Microsoft/BingMapsRESTToolkit
//
// Just install NuGet package named 'BingMapsRESTToolkit'!
// Don't forget to add namespace BingMapsRESTToolkit to your using block!
//
// Now you can get the coordinates of any address by calling the following method
public string GetLocation(string address)
{
string results = "";
string key = "Your Bing API key";
try
{
var r = ServiceManager.GetResponseAsync(new GeocodeRequest()
{
BingMapsKey = key,
Query = address
}).GetAwaiter().GetResult();
if (r != null && r.ResourceSets != null &&
r.ResourceSets.Length > 0 &&
r.ResourceSets[0].Resources != null &&
r.ResourceSets[0].Resources.Length > 0)
{
results = String.Format("Success: {0}:{1}",
(r.ResourceSets[0].Resources[0] as Location).Point.Coordinates[0],
(r.ResourceSets[0].Resources[0] as Location).Point.Coordinates[1]);
}
else
{
results = "No Results Found";
}
}
catch (Exception e)
{
results = "Geocoding Error: " + e.Message;
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment