Skip to content

Bing REST Services .NET library provides API for Bing Maps REST Services like Routes, Locations, Traffic, etc.

License

Notifications You must be signed in to change notification settings

ivanovvitaly/BingRestServices.Net

Repository files navigation

Bing REST Services .NET

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.

Build Status NuGet

Installation

To install via NuGet run the following command in the Package Manager Console

Install-Package BingRestServices

Examples

Your should have valid Bing API Key to use Bing REST Services

Find location by Address (US)

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();

Find location by Address (France)

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();

Find location by Point

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"

Find location by Query

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();

Find location including neighborhoods by Query

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");

Calculate travel distance from DC to NY

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;
...

Calculate fastest route from Eiffel Tower to Louvre Museum

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;
...

Calculate routes from Golden Gate Bridge to Fishermans Wharf

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>();
...

Get all traffic incidents in a specified area

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>();

Get traffic incidents by type and severity and request traffic location codes

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>();

Advanced configuration using code

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.

Advanced configuration using configuration section in .config file

<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(...)

Using service with DI (Ninject example)

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();

Development setup

  1. Open the solution in Visual Studio 2013 and start a build.
  2. Automatic restore should download and install each dependency package.

Unit Tests

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

Frameworks used in the project

Release History

TODO

  • Implement Routes API
  • Implement Locations API
  • Implement User Context Parameters
  • Implement Traffic API
  • Implement Elevations API
  • Implement Imagery API

About Me

Vitaly Ivanov – GitHub - Blog - StackOverlowivanov.vitalii@gmail.com

License

This project is licensed under the terms of the MIT license.

About

Bing REST Services .NET library provides API for Bing Maps REST Services like Routes, Locations, Traffic, etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published