Google direction API: not to translate addresses into English

288 Views Asked by At

I am trying to use the Google Direction API service. My addresses are in Chinese, not in English. If I enter the URL directly into the browser, Google returns Chinese addresses. However, if I include the URL in a Python program, Google will translate the Chinese addresses into English.

In the following, variables a, b, c, and d are four Chinese addresses.

from urllib.parse   import quote
from urllib.request import urlopen
import json

url = 'http://maps.googleapis.com/maps/api/directions/json?'
a = '台中市霧峰區吉峰東路168號'
b = '桃園機場'
c = '台中市中區自由路一段1號'
d = '台中市大里區國光路一段1號'
url = (url +
      'origin=' + quote(a) +
      '&destination=' + quote(b) +
      '&waypoints=optimize:true|' + quote(c) + '|' + quote(d) + '&sensor=false')

print(url)
direction = urlopen(url).read().decode('utf-8')

The addresses in direction are all translated into English addresses. How can I prevent Google from translating addresses?

1

There are 1 best solutions below

0
On BEST ANSWER

You can use a parameter language.

See documentation.

req_url = ("{}origin={}&destination={}&waypoints=optimize:true|{}|{}"
              "&sensor=false&language=zh-TW".format(
                  url, quote(a), quote(b), quote(c), quote(d)))

print(req_url)
direction = urlopen(req_url).read().decode('utf-8')


with open('result.txt', 'w') as ii:
    ii.write(direction.encode('utf-8'))