8.6 Microsoft Bing Data

Using Microsoft Bing API, there are URL templates to get latitude and longitude coordinates for a location by specifying values such as a locality, postal code, and street address.

The response returned by a Locations URL contains one or more Location resources. Each Location resource contains location information that corresponds to the values provided in the request.

To use the Bing Maps APIs you must have a Bing Maps Key, so the first thing to do is navigate to the page Getting a Bing Maps Key and create a Bing Maps Key, following the instructions given on this page.

The Python script shown below searches for the location ‘Central do Brasil’, using the key stored in BingMapsKey.txt file.

# Bing - Find a Location by Query
# https://msdn.microsoft.com/en-us/library/ff701711.aspx
# This script shows how to find the location of Central do Brasil
# (Terminal Central Station, Rio de Janeiro, Brazil) using a query
# to obtain the latitude and longitude coordinates that correspond
# to this location.
# -*- coding: utf-8 -*-

import http.client, urllib.parse
import json

# Read the Bing Maps Key from file 'BingMapsKey.txt'
BingMapsKey = open('BingMapsKey.txt', 'r').read().rstrip("\n")

host = 'dev.virtualearth.net'
path = '/REST/v1/Locations'
locationQuery = 'Central do Brasil'

params = '?query=' + urllib.parse.quote (locationQuery) + \
         '&key=' + BingMapsKey

def get_response ():
    conn = http.client.HTTPSConnection (host)
    conn.request ("GET", path + params)
    response = conn.getresponse ()
    return response.read ()

result = get_response ()
print (json.dumps(json.loads(result), indent=4))

Results can not be displayed due to copyright restrictions.

The featured image shows the script in Visual Studio Community 2017 IDE on Windows 10.


8. External data