地理编码返回错误的经/纬度

时间:2019-04-25 07:08:49

标签: python google-maps google-geocoder

我正在使用此代码对此地址进行地址解析,以获取经度和纬度,但返回的坐标错误。

import requests
address = '50 Bega St, Tathra NSW 2550, Australia'
response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address= address &key= ****')

resp_json_payload = response.json()

print(resp_json_payload)
print(resp_json_payload['results'][0]['geometry']['location'])

预期结果:-36.730478 149.985058

实际结果:“纬度”:37.09024,“ lng”:-95.712891

1 个答案:

答案 0 :(得分:0)

  

在Python 3.6上编译的代码,您可能想对旧版本的Python使用不同的url解析库。

import requests
import urllib.parse
address = '50 Bega St, Tathra NSW 2550, Australia'
api_key = 'your_api_key'
encoded_address = urllib.parse.quote(address)
response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address='+encoded_address+'&key='+api_key)

resp_json_payload = response.json()

print(resp_json_payload)
print(resp_json_payload['results'][0]['geometry']['location'])

希望这会有所帮助。