小编Joh*_*nie的帖子

将纬度和经度放入距离矩阵中,python中的谷歌地图API

我有一个以下格式的Pandas 数据框dataframe。 在此处输入图片说明

现在我想把所有的起始纬度和起始经度对放到原点,把所有的结束纬度和结束经度对放到目的地。我想得到每一行的距离和持续时间,如下所示。预期输出:

    Rental Id | Distance | Duration | Status
0   51649420    0          0          OK
1   51649421    959        214        OK
2  
...
15
Run Code Online (Sandbox Code Playgroud)

我尝试遵循两种方法,但它们都给了我超时错误。

方法一:

import googlemaps
from pandas.io.json import json_normalize
gmaps = googlemaps.Client(key='my API key')
for i in range (0,15):
origins = (journeydf['Start Latitude'][i], journeydf['Start Longitude'][i])
destinations = (journeydf['End Latitude'][i], journeydf['End Longitude'][i])
matrix = gmaps.distance_matrix(origins, destinations, mode="bicycling")
matrixdf = json_normalize(matrix,['rows','elements'])
matrixdf['Rental Id']=journeydf['Rental Id']
Run Code Online (Sandbox Code Playgroud)

方法二:

import urllib, json, time
import pandas as pd

def google(lato, lono, …
Run Code Online (Sandbox Code Playgroud)

python google-maps numpy latitude-longitude pandas

5
推荐指数
1
解决办法
9367
查看次数

标签 统计

google-maps ×1

latitude-longitude ×1

numpy ×1

pandas ×1

python ×1