Bing REST Services .NET library provides API for Bing Maps REST Services like Routes, Locations, Traffic and other Bing Maps REST services to perform tasks such as geocoding an address, creating a route, etc.
To install via NuGet run the following command in the Package Manager Console
Install-Package BingRestServices
Your should have valid Bing API Key to use Bing REST Services
var parameters = new FindLocationByAddressParameters();
parameters.Address = GeoAddress.CreateAddress(
"1 Microsoft Way",
"Redmond",
"WA",
"98052",
"US");
var bingLocations = new BingLocations(new BingConfiguration("API_KEY"));
var response = await bingLocations.FindLocationAsync(parameters);
var location = response.ResourceSets.First().Resources.OfType<Location>().First();
var parameters = new FindLocationByAddressParameters();
parameters.Address = new GeoAddress();
parameters.Address.CountryRegion = "FR";
parameters.Address.PostalCode = "75007";
parameters.Address.Locality = "Paris";
parameters.Address.AddressLine = "Avenue Gustave Eiffel";
var bingLocations = new BingLocations(new BingConfiguration("API_KEY"));
var response = await bingLocations.FindLocationAsync(parameters);
var location = response.ResourceSets.First().Resources.OfType<Location>().First();
var parameters = new FindLocationByPointParameters();
parameters.Point = GeoPoint.Create(47.64054, -122.12934);
var bingLocations = new BingLocations(new BingConfiguration("API_KEY"));
var response = await bingLocations.FindLocationAsync(parameters);
var location = response.ResourceSets.First().Resources.OfType<Location>().First();
// location.Name: "Microsoft Way, Redmond, WA 98052"
var parameters = new FindLocationByQueryParameters();
parameters.Query = GeoAddress.CreateLandmark("Eiffel Tower");
var bingLocations = new BingLocations(new BingConfiguration("API_KEY"));
var response = await bingLocations.FindLocationAsync(parameters);
var eiffelTower = response.ResourceSets.First().Resources.OfType<Location>().First();
var parameters = new FindLocationByQueryParameters();
parameters.IncludeNeighborhood = IncludeNeighborhood.Include;
parameters.Query = GeoAddress.CreateLandmark("Brookyln New York"); // with misprint
var bingLocations = new BingLocations(new BingConfiguration("API_KEY"));
var response = await bingLocations.FindLocationAsync(parameters);
var locations = response.ResourceSets.First().Resources.OfType<Location>();
var brooklyn = locations.First(p => p.EntityType == "PopulatedPlace");
var neighborhoods = locations.Where(p => p.EntityType == "Neighborhood");
var parameters = new CalculateRoutesParameters();
parameters.TravelMode = TravelMode.Driving;
parameters.WayPoints = new IGeoLocation[] { GeoPoint.Create(38.890366, -77.031955), GeoPoint.Create(40.714545, -74.007139) };
var bingRoutes = new BingRoutes(new BingConfiguration("API_KEY"));
var response = await bingRoutes.CalculateRoutesAsync(parameters);
var route = response.ResourceSets.First().Resources.OfType<Route>().First();
var travelDistance = route.TravelDistance;
...
var parameters = new CalculateRoutesParameters();
parameters.TravelMode = TravelMode.Walking;
parameters.RouteOptimization = RouteOptimization.Distance;
parameters.MaxSolutions = MaxSolutions.One;
parameters.WayPoints = new IGeoLocation[] { GeoAddress.CreateLandmark("Eiffel Tower"), GeoAddress.CreateLandmark("louvre museum") };
var bingRoutes = new BingRoutes(new BingConfiguration("API_KEY"));
var response = await bingRoutes.CalculateRoutesAsync(parameters);
var route = response.ResourceSets.First().Resources.OfType<Route>().First();
var travelDuration = route.TravelDuration;
...
var parameters = new CalculateRoutesParameters();
parameters.TravelMode = TravelMode.Transit;
parameters.TransiteTimeType = TransiteTimeType.Departure;
parameters.DesireTransiteTime = DateTime.Today.AddHours(3);
parameters.WayPoints = new IGeoLocation[] { GeoAddress.CreateLandmark("Golden Gate Bridge"), GeoAddress.CreateLandmark("Fishermans Wharf") };
var bingRoutes = new BingRoutes(new BingConfiguration("API_KEY"));
var response = await bingRoutes.CalculateRoutesAsync(parameters);
var routes = response.ResourceSets.First().Resources.OfType<Route>();
...
var mapArea = new MapArea(37, -105, 45, -94);
var parameters = new TrafficIncidentsParameters(mapArea);
var bingTraffic = new BingTraffic(new BingConfiguration("API_KEY"));
var response = await bingTraffic.GetTrafficIncidents(parameters);
var trafficIncidents = response.ResourceSets.First().Resources.OfType<TrafficIncident>();
var mapArea = new MapArea(37, -105, 45, -94);
var parameters = new TrafficIncidentsParameters(mapArea);
parameters.IncludeLocationCodes = true;
parameters.Severity = new[] { Severity.Minor, Severity.Moderate };
parameters.TrafficIncidentTypes = new[] { TrafficIncidentType.Construction };
var bingTraffic = new BingTraffic(new BingConfiguration("API_KEY"));
var response = await bingTraffic.GetTrafficIncidents(parameters);
var trafficIncidents = response.ResourceSets.First().Resources.OfType<TrafficIncident>();
var jsonConfiguration = BingConfiguration.CreateJsonOutputConfiguration("API_KEY");
var bingRoutes = new BingRoutes(jsonConfiguration);
var configuration = new BingConfiguration("API_KEY");
configuration.OutputFormat = "xml";
configuration.ErrorDetail = true;
configuration.Culture = "en-US";
...
var bingRoutes = new BingRoutes(configuration);
Configuration parameters description can be found here Common Parameters and Types and here Bing Maps REST URL Structure. All parameters can be configuration through BingConfiguration class.
<configuration>
<configSections>
<section name="bingConfiguration" type="BingRestServices.Configuration.BingConfigurationSection, BingRestServices" />
</configSections>
...
<bingConfiguration key="API_KEY"
baseUrl="http://dev.virtualearth.net/REST/v1/"
output="json">
</bingConfiguration>
...
<configuration>
This way service can be instantiated without configuration object
var bingRoutes = new BingRoutes();
await bingRoutes.CalculateRoutesAsync(...)
With configuration section in .config file
kernel.Bind<IBingRoutes>().To<BingRoutes>().InRequestScope();
Without configuration section
kernel.Bind<IBingRoutes>().To<BingRoutes>()
.WithConstructorArgument("configuration", _ => new BingConfiguration("API_KEY"))
.InRequestScope();
- Open the solution in Visual Studio 2013 and start a build.
- Automatic restore should download and install each dependency package.
I use NUnit and Moq for Unit Tests. In order test Bing Services you should have Bing API Key. Put the API Key into bingConfiguration section in the App.config file BingRestServices.Tests project
<bingConfiguration key="API_KEY"
baseUrl="http://dev.virtualearth.net/REST/v1/"
output="json"
culture="en-US">
Run unit tests from Visual Studio or using nunitlite-runner
- 1.0.0
- Introduced BingRoutes service for Routes API
- 1.1.0
- Introduced BingLocations service for Locations API
- 1.2.0
- Introduced BingTraffic service for Traffic API
- Implement Routes API
- Implement Locations API
- Implement User Context Parameters
- Implement Traffic API
- Implement Elevations API
- Implement Imagery API
Vitaly Ivanov – GitHub - Blog - StackOverlow – [email protected]
This project is licensed under the terms of the MIT license.