Python – How to use the Bing Maps API to get directions

bing-mapspython

So I have this python program that has input longitude latitude and destination

The longitude and latitude are the current long and lat of the user, and the destination is a string of the place they want to go.

How would I use Bing Map API in my python program to generate a string of directions?

It is kind of like this:

longitude = -122.019943
latitude = 37.285989
destination = "1427 Alderbrook Ln San Jose CA 95129"
# Some Bings Map API Magic that generates directions in a string
print directions

And output would be: Turn right on East DeAnza Blvd…. or something like that

Could anyone give me any guidance? Since Google Maps API didn't work out for me and I need to use Bing Maps API. Any help would be greatly appreciated.

Best Answer

First off you will want to use the Bing Maps REST Routing Service: https://msdn.microsoft.com/en-us/library/ff701717.aspx

Creating the request URL is pretty straight forward and the same regardless of the programming language you use. For your example, to calculate driving directions for your locations you will create a route request URL like this:

http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=37.285989,-122.019943&wp.1=1427%20Alderbrook%20Ln%20San%20Jose%20CA%2095129&key=YOUR_BING_MAPS_KEY

Notice that the street address has %20 in place of the spaces. This is called URL encoding and is a best practice. This is easy to do in python but wanted to point it out now so you know why it's in the code sample below.

The following code sample encodes the destination, creates a URL and prints the response from the Bing Maps service. Note that I'm using Python 3.4. Depending on the version of Python you are using the code may vary slightly.

import urllib.request

# Your Bing Maps Key 
bingMapsKey = "YOUR_BING_MAPS_KEY"

# input information
longitude = -122.019943
latitude = 37.285989
destination = "1427 Alderbrook Ln San Jose CA 95129"

encodedDest = urllib.parse.quote(destination, safe='')

routeUrl = "http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=" + str(latitude) + "," + str(longitude) + "&wp.1=" + encodedDest + "&key=" + bingMapsKey

request = urllib.request.Request(routeUrl)
response = urllib.request.urlopen(request)
print (response.read())

Now this prints out response from the service as a bunch of text which is actually a bunch of JSON. We will need to parse the JSON so we can extract the information that we want. To do this you will need to know the structure of the JSON response which is documented here: https://msdn.microsoft.com/en-us/library/gg636957.aspx

To parse the response you will need to import the JSON library for Python. Once you do that you can load the raw data as a JSON object and then step through the JSON object tree to grab the values you want. Since you are only calculating route between two points there will only be 1 route leg in the response. The route leg contains an array of itinerary items which contain the route step information. Here is an updated code sample that loops through each item and prints the instructions line by line:

import urllib.request
import json

# Your Bing Maps Key 
bingMapsKey = "YOUR_BING_MAPS_KEY"

# input information
longitude = -122.019943
latitude = 37.285989
destination = "1427 Alderbrook Ln San Jose CA 95129"

encodedDest = urllib.parse.quote(destination, safe='')

routeUrl = "http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=" + str(latitude) + "," + str(longitude) + "&wp.1=" + encodedDest + "&key=" + bingMapsKey

request = urllib.request.Request(routeUrl)
response = urllib.request.urlopen(request)

r = response.read().decode(encoding="utf-8")
result = json.loads(r)

itineraryItems = result["resourceSets"][0]["resources"][0]["routeLegs"][0]["itineraryItems"]

for item in itineraryItems:
    print(item["instruction"]["text"])