Distance table google maps api and python

Asked

Viewed 885 times

1

Hello,

I am using the Google Maps API to check the distance between two points, for this I have two local.csv files and coordinators.csv.

The ultimate goal is to know the distance of the locations for each of the coordinations.

If there are 3 sites and 2 coordinations, the expected result was to be:

Local1, Coordenacao1
Local1, Coordenacao2
Local2, Coordenacao1
Local2, Coordenacao2
Local3, Coordenacao1
Local3, Coordenacao2

But the code below is only returning the first coordination item:

Local1, Coordenacao1
Local1, Coordenacao2

I believe the error lies in the use of for

import googlemaps
import csv

gmaps = googlemaps.Client(key='TOKEN')

coluna_participante = 0
coluna_coordenacao = 0

entrada_local = open('arquivos/locais.csv', 'r')
entrada_coordenacao = open('arquivos/coordenacoes.csv', 'r')
saida = open('arquivos/saida.csv', 'w', newline='')

try:
    ler_local = csv.reader(entrada_local)
    ler_coordenacao = csv.reader(entrada_coordenacao)
    escrever = csv.writer(saida)
    for row in list(ler_coordenacao)[1:]:
        coordenadas_coordenacao = row[coluna_coordenacao]
        for row in list(ler_local)[1:]:
            coordenadas_local = row[coluna_local]
            distancia = gmaps.distance_matrix(coordenadas_local, coordenadas_coordenacao)
            print(distancia)

finally:
    entrada_local.close()
    entrada_coordenacao.close()
    saida.close()

I would also like to get the result in table format, in the output.csv file in the following template:

local1, distancia(1,1), distancia(1,2)
local2, distancia(2,1), distancia(2,2)
local3, distancia(3,1), distancia(1,2)

What changes should I make?

  • The resolution can be accessed here: http://stackoverflow.com/questions/43702740/table-of-distances-with-python-and-google-maps-api

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.