question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling `from_geocode` occasionally produces "list index out of range" error

See original GitHub issue

I’ve been using gmplot to draw running routes (via tcx files). Until recently I could loop through a file list and generate the new html plots.

But now the loop will seemingly-spontaneously fail and generate the following error:

 40             'http://maps.googleapis.com/maps/api/geocode/json?address="%s"' % location_string)
 41         geocode = json.loads(geocode.text)

—> 42 latlng_dict = geocode[‘results’][0][‘geometry’][‘location’] 43 return latlng_dict[‘lat’], latlng_dict[‘lng’] 44

IndexError: list index out of range

I was able to isolate it to the call for gmap = gmplot.GoogleMapPlotter.from_geocode("San Francisco")

For example, running the following code will result in throwing an error at some point (usually in the 8-14 range):

for i in range(25):
    print(i)
    gmap = gmplot.GoogleMapPlotter.from_geocode("San Francisco")

I can wrap it in a try/except but that merely delays the break.

My naive guess is that I’m breaking some API limit.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sandrotosicommented, Oct 18, 2018

fixed with:

@@ -34,14 +35,14 @@ class GoogleMapPlotter(object):
         self._infowindows = []
 
     @classmethod
-    def from_geocode(cls, location_string, zoom=13):
-        lat, lng = cls.geocode(location_string)
-        return cls(lat, lng, zoom)
+    def from_geocode(cls, location_string, apikey, zoom=13):
+        lat, lng = cls.geocode(location_string, apikey)
+        return cls(lat, lng, zoom, apikey)
 
     @classmethod
-    def geocode(self, location_string):
+    def geocode(self, location_string, apikey):
         geocode = requests.get(
-            'http://maps.googleapis.com/maps/api/geocode/json?address="%s"' % location_string)
+            'https://maps.googleapis.com/maps/api/geocode/json?address="%s"&key=%s' % (location_string, apikey))
         geocode = json.loads(geocode.text)
         latlng_dict = geocode['results'][0]['geometry']['location']
         return latlng_dict['lat'], latlng_dict['lng']

ie you’re forced to pass and API key (as gmaps forces you to)

2reactions
hlongmorecommented, Jun 16, 2019

Any chance this can get merged into the official release? Or do I need to fork yet another project to fix bugs that have had a solution sitting around in some other fork for a while?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Google Geocoding API - IndexError: list index out of ...
I'm having a problem with the API where I keep getting an error. Let me show the code, first. # import necessary modules...
Read more >
What Does the Python "List index out of range" Error Mean?
When Python throws a "list index out of range" error, it means you tried to slice the list beyond its last index. Python...
Read more >
How to fix 'Sys.argv [1], indexerror: list index out of range ...
It means you are calling for the position of an element of the list higher than the amount of elements. If you have...
Read more >
Geocoding Service | Maps JavaScript API - Google Developers
"ERROR" indicates that the request timed out or there was a problem contacting the Google servers. The request may succeed if you try...
Read more >
Welcome to GeoPy's documentation! — GeoPy 2.3.0 ...
Refer to the specific geocoder's initializer doc for a list of parameters which that ... (e.g. by returning Too Many Requests 429 HTTP...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found