Microsoft Bing Maps Distance Matrix API
Blog|by Jamie Maguire|12 December 2017
Introduction
Microsoft have been busy, working hard on the Bing Maps v8 Web Control and adding rich new functionality. One of the more recent additions to the Bing Maps ecosystem is the Distance Matrix API.
The API was just released at Microsoft Ignite in October and allows you to generate travel time and distances (with the help of the Bing Maps Route API) for a given set of origins and destinations.
It can also factor in predictive traffic information when generating times thereby allowing you to avoid any potential delays. Some of the other features include, but are not limited to:
- Calculating 1 days’ worth of travel in 15 minute intervals
- Support for multiple transport modes
- Support for GET and POST requests
- Asynchronous support (for larger requests)
- JSON and XML support
- Option to cache results for up to 72 hours
In this blog post:
- we introduce the Bing Distance Matrix API
- identify some of the applications
- discuss how the Bing Distance Matrix API helps solve real world problems
- walk through some sample code
First, a few concepts:
Distance Matrix
A distance matrix is a 2d array that contains N origins and N destinations.
Way Point
A geographical location defined by longitude and latitude used for navigational purposes.
Travel Mode
This is the mode of travel for a given route. This can be Driving, Public Transit or Walking.
Applications
A distance matrix has numerous applications in routing and fleet management and can be applied to many sectors such as Retail, Logistics, Manufacturing and Property / Real Estate.
Implementing a distance matrix allows you to:
-
-
- determine arrival times based on start times
- sort search results by their travel distance or time
- calculate the different in commuter times between locations
- cluster data based on travel time/distance. E.g. Find me all restaurants within a 3-mile radius of the property you’re about to purchase.
- …and much more!
-
These features can be important if you need to consider time windows, have multiple pickup and delivery locations or split deliveries.
One of the most common applications of the distance matrix is to help power algorithms related to logistics problems, specifically the Vehicle Routing (VRP) and Travelling Salesman Problems (TSP) (route optimisation).
The Traveling Salesman Problem (TSP)
The travelling salesman problem, which was first formulated in 1930, asks the following question:
Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?”
A problem like this has many variables, and it’s out-with the scope of this blog post to go into mathematics, linear programming and computation required to arrive at such answers. An in depth explanation of the TSP can be found here if you’re interested.
How does the Distance Matrix API to solve this problem?
The Bing Maps Distance Matrix API shields you from the complexity of this problem and helps solve it for you with ease.
By encapsulating complex algorithms in easy to use endpoints, you can quickly develop solutions in a language of your choice and integrate TSP solutions with your existing tech stack or business application.
You supply a set of Origins, Destinations and Travel Mode, invoke the Distance Matrix API, and a distance matrix will be returned that contains Travel Distances and Travel Durations.
Armed with this data you can then identify an optimum route for your fleet or sales team.
An Example
In this example, we have a salesman that has several client meetings across the UK. He’s from London and needs to visit the following places in one day.
-
-
- Leeds
- York
- Liverpool
-
A solution that leverages the Bing Distance Matrix API can help optimise our salesman journey times by prompting the salesman as to which clients are closest.
In the following screen shot, a request has been constructed in Postman with the following parameters:
This request is then sent to the Distance Matrix API Endpoint and returns the following response which you can see in the code extract below:
The JSON is straight forward enough to read. The key nodes are:
-
-
- Origins
- Destinations
- Results
-
The Origin and Destination nodes are self-explanatory – these contain the starting point and destinations that are relevant to our salesman.
Results Node
In the Results node however, you can see the travelDisance and travelDuration values have been calculated for each destination by the Distance Matrix API.
As our fictitious salesman completes each meeting, his mobile CRM tool, powered by the Distance Matrix API, can auto-suggest his next closest meeting, thereby allowing him to focus on value-add tasks as opposed to journey planning.
“Under the hood”, you can query this JSON in your application, sort and display the results in whichever way you see fit. This just one way that a Bing Distance Matrix API can help businesses run more efficiently.
Another example – Property and Real Estate
Imagine for a minute that you’re working on a mobile application for a property / real estate business. When searching for properties in the mobile application, users must be able to plan a potential commute from the property they’re currently viewing.
With the Distance Matrix API, you can supply the latitude/longitude of the property and the respective destinations (office or train station for example) – along with the mode of travel (driving, public transit or walking) and the API will return data that allows you to determine the quickest routes.
You might also want to further enrich your datasets by introducing the Spatial Data Services API to identify Points of Interest Data (POI).
Predictive Intelligence
Another layer of intelligence can be applied by leveraging predictive traffic data which can help you further provide more accurate timing estimates.
If this is something that you’re interested in, the Distance Matrix Histogram endpoint is something you should explore.
Summary
In this blog post, we’ve looked at the Bing Distance Matrix API and explored some of the features and what’s possible with it. We’ve seen how easy it is to consume the Distance Matrix API using a tool like Postman.
Alternatively, if you’re interested in seeing how the API can be consumed using .NET, you can check out the Bing Maps REST Toolkit for .NET project on GitHub here
Are you using Bing Maps in any of your solutions?
~~~
For reference:
You’ll need a Bing Maps Account and Key prior to making any requests, you can get one for free here.
The entire request that was sent to the Distance Matrix API endpoint via Postman:
You might also be wondering how to identify the latitude and longitude of each location, you can get this information by making a request to the following Endpoint:
http://dev.virtualearth.net/REST/v1/Locations?q=YOUR_LOCATION.
Get expert licensing and pricing advice from the Grey Matter Bing Maps mapping team: mapping@greymatter.com or call +44 (0)1364 655133.
Contact Grey Matter
If you have any questions or want some extra information, complete the form below and one of the team will be in touch ASAP. If you have a specific use case, please let us know and we'll help you find the right solution faster.
By submitting this form you are agreeing to our Privacy Policy and Website Terms of Use.
Jamie Maguire
http://www.jamiemaguire.netSoftware Architect, Consultant, Developer, and Microsoft AI MVP. 15+ years’ experience architecting and building solutions using the .NET stack. Into tech, web, code, AI, machine learning, business and start-ups.
Related News
JavaScript Day 2024
Thu 24 October 2024 2:00 pm - 6:00 pm GMT
Are you ready to dive deep into the world of JavaScript and TypeScript? Mark your calendars for 24 October 2024 because JetBrains JavaScript Day 2024 is back for its fourth year, and it’s going to be bigger and better than...
JetBrains GameDev Days 2024
9 - 10 October 2024
A Must-Attend Event for Game Developers Are you passionate about game development? Then attend JetBrains GameDev Days 2024, a free, live virtual event dedicated to all things game development. This hybrid event will take place on 9-10 October 2024 and...
.NET Days Online 2024
25 - 26 September 2024 10:30 am - 5:45 pm BST
.NET Developer Event Hosted by JetBrains Mark your calendars for 25-26 September 2024, as JetBrains hosts the highly anticipated .NET Days Online 2024. This free, two-day virtual event promises to be a treasure trove of knowledge and networking for .NET...
SPECIAL OFFER: 20% Off Embarcadero
Embarcadero offer To celebrate the upcoming 12.2 version update, Embarcadero is offering 20% off RAD Studio, Delphi and C++Builder. After the release, if you are on Update Subscription, your licence will be automatically updated to 12.2. Terms and conditions: This...