1

I am trying to find the distance between multiple addresses in a CSV file. However, when trying to single out the issue, the output returns the same incorrect address no matter when I put in the lat/long, or physical address.

import sys
sys.path.append('C:\Python36\Lib\site-packages')
import google maps
gmaps = googlemaps.Client(key='-')
origins = ['26.650032', '-81.842073']
destinations = ['26.617167', '-81.860763']
JTC = gmaps.distance_matrix('origins'+", Florida", 'destinations'+", Florida", units="imperial")
print(JTC)


{'destination_addresses': ['633 N Orange Ave, Orlando, FL 32801, USA'], 'origin_addresses': ['933 45th St, West Palm Beach, FL 33407, USA'], 'rows': [{'elements': [{'distance': {'text': '167 mi', 'value': 269040}, 'duration': {'text': '2 hours 27 mins', 'value': 8822}, 'status': 'OK'}]}], 'status': 'OK'}

.....

origins = ['3438 Lantana St, 33916, USA']
destinations = ['2560 Kennesaw St, 33901, USA']
JTC = gmaps.distance_matrix('origins'+", Florida", 'destinations'+", Florida", units="imperial")
print(JTC)


{'destination_addresses': ['633 N Orange Ave, Orlando, FL 32801, USA'], 'origin_addresses': ['933 45th St, West Palm Beach, FL 33407, USA'], 'rows': [{'elements': [{'distance': {'text': '167 mi', 'value': 269040}, 'duration': {'text': '2 hours 27 mins', 'value': 8822}, 'status': 'OK'}]}], 'status': 'OK'}

.....

Am I doing something wrong with my code?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Can you get your interpreter to print out `'origins'+", Florida"` and `'destinations'+", Florida"`? Do those strings contain any coordinates at all? – castle-bravo Mar 18 '18 at 04:19
  • I cannot get them to print separately. Googlemaps converts whatever origin and destination into coordinates and then returns them. I tried being more and less specific, but I never get the desired results. – Jaime Argueta Mar 18 '18 at 14:49
  • It just looks to me like you're sending the string `'origins, Florida'` instead of what I assume you want to send which is a string like `'26.650032, -81.842073, Florida'`. Does that sound right? Do you want some tips on how to format the string? – castle-bravo Mar 18 '18 at 15:02
  • Please :) Thank you very much for your help as I am trying to figure all of this out! Also if you look at the below suggestions, I have tried his method and commented on its results if that helps us move forward. Again thanks! – Jaime Argueta Mar 19 '18 at 02:06

1 Answers1

0

Try the following for coordinates:

JTC = gmaps.distance_matrix('%f, %f, Florida' % origins, 
                            '$f, %f, Florida' % destinations, 
                            units="imperial")

If have addresses, you need to format the string differently:

JTC = gmaps.distance_matrix(origins[0], destinations[0],
                            units="imperial")

I think Google maps is smart enough to figure out that these addresses are in Florida.

You've stored the addresses inside lists; if it was just a string, you could concatenate like so: origins + ', Florida'.

Let me know if that helps.

castle-bravo
  • 1,389
  • 15
  • 34
  • So when I tried to input the coordinate, it gave me different results: `{'destination_addresses': ['81 FL-26, Gainesville, FL 32641, USA'], 'origin_addresses': ['81 FL-26, Gainesville, FL 32641, USA'], 'rows': [{'elements': [{'distance': {'text': '1 ft', 'value': 0}, 'duration': {'text': '1 min', 'value': 0}, 'status': 'OK'}]}], 'status': 'OK'}` I removed Florida, it returned nothing: `'destination_addresses': [''], 'origin_addresses': [''], 'rows':[{'elements': [{'status': 'NOT_FOUND'}]}], 'status': 'OK'}` – Jaime Argueta Mar 19 '18 at 01:48
  • @JaimeArgueta I think that your best bet is to look at the Google maps Python API documentation, and try to figure out exactly what address string formatting they require. You'll also want to check out the Python documentation on string formatting, cause there are lots of little tricks that you can use to make your data match up with whatever Google requires. I don't say this to brush you off; it'll help you out in the long run. Failing that, I think you could contact Google support. – castle-bravo Mar 19 '18 at 12:52
  • No worries. I'll figure it out, I think I have an idea of where to head. Thank you for your help! – Jaime Argueta Mar 19 '18 at 18:59