nexla
nexla

Reputation: 453

Google maps python next_page_token doesn't work

I send a request via google maps library and I get a result of 20 places.

My code looks like that

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

x = gmaps.places(query=['restaurants', 'bakery', 'cafe', 'food'],location='40.758896, -73.985130',radius=10000)

print len(x['results']) # outputs 20 (which is maximum per 1 page result)

and afterwards I want to request a 2nd page and I use it I suppose correctly.

print gmaps.places(page_token=x['next_page_token'].encode())

and it returns a weird error

    File "/Users/././testz.py", line 15, in <module>
    print gmaps.places(page_token=x['next_page_token'].encode())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlemaps/client.py", line 356, in wrapper
    result = func(*args, **kwargs)
TypeError: places() takes at least 2 arguments (2 given)

I use .encode() because the 'next_page_token' outputs in unicode and googlemaps requires 'str'.

Any idea what am I doing wrong and how can I fix it ?

Upvotes: 5

Views: 2582

Answers (1)

user2314737
user2314737

Reputation: 29317

It should work if you pass all parameters again (in addition to the page_token). Note that you need to add a couple of seconds delay to allow the token to be validated on Google's servers.

import googlemaps
import time

k="your key here"

gmaps = googlemaps.Client(key=k)

params = {
    'query': ['restaurants', 'bakery', 'cafe', 'food'],
    'location': (40.758896, -73.985130),
    'radius': 10000
}

x = gmaps.places(**params)
print len(x['results']) # outputs 20 (which is maximum per 1 page result)
print x['results'][0]['name']

params['page_token'] = x['next_page_token']

time.sleep(2)
x1 = gmaps.places(**params)

print len(x1['results'])
print x1['results'][0]['name']

Upvotes: 6

Related Questions