Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11295

Google Maps API not retrieving all stores result

I wrote up a quick script to fetch all stores (tried to do so anyway) provided a zipcode, it looks like this:

from googleplaces import GooglePlaces, types, lang

API_KEY = "MYKEY"

google_places = GooglePlaces(API_KEY)


query_result = google_places.nearby_search(location="94563", keyword="store", radius=50000)

if query_result.has_attributions:
    print query_result.html_attributions

for place in query_result.places:
    print place.name

These are the results that I get:

Apple Store
Stonestown Galleria
Lawrence Hall of Science
Fentons Creamery
Nordstrom
The North Face
Amoeba Music
Safeway
Rockridge Market Hall
City Beer Store
Best Buy
City Lights Booksellers & Publishers
Macy's
Barnes & Noble
Rainbow Grocery
Target
Urban Outfitters
The UPS Store
AT&T
Marshalls

But if we head over to maps.google.com we can query for the same stores and this is what we get:

stores sample

We notice that there are many stores in this result set that are not queried from the API. Not sure what I'm doing wrong.

Upvotes: 1

Views: 303

Answers (1)

Ryan
Ryan

Reputation: 3709

When more than 20 results come up with a nearby search, a next_page_token is also returned by the API, and a separate call must be made to retrieve them [Reference]. I don't know whether the googleplaces package you're using is able to do that, but that is most likely the reason you're getting exactly 20 results; the rest are there, you just need to call the API again to get them.

My recommendation would be to ditch the package and instead deal with Google's API directly. Here is some helper code to get you started doing that. You will need to download and install geopy if you don't have it already.

import json
import urllib
import time
from geopy.geocoders import Nominatim
geolocator = Nominatim()
l = geolocator.geocode('94563') #enter the zip code you're interested in to get lat/long coords


longitude = l.longitude
latitude = l.latitude

resultslist = []
url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='+str(latitude)+','+str(longitude)+'&radius=50000&types=store&key=<put your key here>' #construct URL, make sure to add your key without the <>
count = 0
ps= json.loads(urllib.urlopen(url).read())
for i in ps['results']:
    #parse results here
    resultslist.append(i)
    count += 1
if ps['next_page_token']: 
    while True:
        time.sleep(2)
        npt = ps['next_page_token']

        url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='+str(latitude)+','+str(longitude)+'&radius=50000&types=store&key=<yourkey>&pagetoken='+str(npt)

        ps= json.loads(urllib.urlopen(url).read())

        for i in ps['results']:
            resultslist.append(i)
            #parse results here
            count += 1

        try:
            ps['next_page_token']
        except:
            break

print 'results returned:',count

Upvotes: 3

Related Questions