谷歌地图API不会“查找”地址,即使它们在网络上正常工作

问题描述 投票:0回答:1

尝试编写一个使用Google Distance Matrix API来计算到单个位置的距离的python脚本。由于某种原因,它适用于某些地址,而不适用于其他地址,我无法弄清楚为什么会这样。

我使用的代码是这个的修改版本:https://gist.github.com/olliefr/407c64413f61bd14e7af62fada6df866

我只是更改了它,所以它在表单上列出了一个列表(第50/51行):

['Nordmarksvägen 15', 'Häggvägen 25B', 'Nordenskiöldsgatan 78', 'Oskarsvägen 
7B', 'Norrviksvägen 22A']

并返回每个条目到我的点之间的距离。在这里我们可以清楚地看到来自我的origin数组的一些条目不起作用?

这是我的“结果数组”的打印,我们清楚地看到5个条目中的3个不起作用。

 to Kungstensgatan 6, 114 25 Stockholm, Sweden: status = NOT_FOUND
 to Kungstensgatan 6, 114 25 Stockholm, Sweden: status = NOT_FOUND
 to Kungstensgatan 6, 114 25 Stockholm, Sweden: status = NOT_FOUND
['13.5 km Nordmarksvägen 15, 123 72 Farsta, Sweden', '3.5 km 
Nordenskiöldsgatan 78, 115 21 Stockholm, Sweden']  

要在这里添加完整的代码,以防万一有任何混淆:

import json
import requests
import sys
# Google Distance Matrix Python Demo
# ==================================
#
# H


distances = []
org = []

if __name__ == '__main__':

# The API key must be provided on the command line, abort otherwise. 
api_key = 'Just gonna keeps this away for now'


# Google Distance Matrix base URL to which all other parameters are attached
base_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?'

# Google Distance Matrix domain-specific terms: origins and destinations
origins = ['Nordmarksvägen 15', 'Häggvägen 25B', 'Nordenskiöldsgatan 78', 'Oskarsvägen 7B', 'Norrviksvägen 22A']
destinations = ['Kungstensgatan 6']

# Prepare the request details for the assembly into a request URL
payload = {
    'origins' : '|'.join(origins),
    'destinations' : '|'.join(destinations), 
    'mode' : 'walking',
    'api_key' : api_key
}

# Assemble the URL and query the web service
r = requests.get(base_url, params = payload)

# Check the HTTP status code returned by the server. Only process the response, 
# if the status code is 200 (OK in HTTP terms).
if r.status_code != 200:
    print('HTTP status code {} received, program terminated.'.format(r.status_code))
else:
    try:
        # Try/catch block should capture the problems when loading JSON data, 
        # such as when JSON is broken. It won't, however, help much if JSON format
        # for this service has changed -- in that case, the dictionaries json.loads() produces
        # may not have some of the fields queried later. In a production system, some sort
        # of verification of JSON file structure is required before processing it. In XML
        # this role is performed by XML Schema.
        x = json.loads(r.text)

        # Now you can do as you please with the data structure stored in x.
        # Here, we print it as a Cartesian product.
        for isrc, src in enumerate(x['origin_addresses']):
            for idst, dst in enumerate(x['destination_addresses']):
                row = x['rows'][isrc]
                cell = row['elements'][idst]
                if cell['status'] == 'OK':
                    distances.append(cell['distance']['text'] +" "+ src)

                else:
                    print('{} to {}: status = {}'.format(src, dst, cell['status']))
        print(distances)

    except ValueError:
        print('Error while parsing JSON response, program terminated.')
python google-maps google-distancematrix-api
1个回答
0
投票

实际上没有找出确切原因,但我发现如果您提供州和国家等其他信息而不仅仅是地址,它就会开始工作!

例如:

origins = ['Nordmarksvägen 15']

应该写

origins = ['Nordmarksvägen 15, State, Country' ]

这解决了我的问题,现在所有条目都正常工作!

© www.soinside.com 2019 - 2024. All rights reserved.