1

I want to make code in Python in GIS to download POIs within a specific radius.

Can anyone check my code for the error? After running this code. The Result is only [].

import requests
import json
import os
import sys
from datetime import datetime
import xlrd
import time

# Set up API credentials
api_key = 'Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

# Define the location and radius
location = '39.169061,21.569649' 
radius = 100  # Radius in meters

# Send a request to the Nearby Search API
url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius={radius}&key={api_key}'
response = requests.get(url)
data = response.json()

# Process the API response
places = data['results']

while 'next_page_token' in data:
    next_page_token = data['next_page_token']
    url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius={radius}&key={api_key}&pagetoken={next_page_token}'
    response = requests.get(url)
    data = response.json()
    places.extend(data['results'])

# Save the data
with open('poi_data.json', 'w') as file:
    json.dump(places, file)
3
  • What do you mean by []? empty? Commented Jun 6, 2023 at 14:30
  • 1
    Check the data['status']. developers.google.com/maps/documentation/places/web-service/… Commented Jun 6, 2023 at 21:27
  • @Eli Switzer: Just checking in to see if my answer below worked for you? Commented Jun 14, 2023 at 17:37

2 Answers 2

2

You need to make the url variables value be f-string. Change them to this and you'll be sending properly formatted urls:

url = f'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius={radius}&key={api_key}'

url = f'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius={radius}&key={api_key}&pagetoken={next_page_token}'

Also, your Lat/Long appear to be backwards, unless the mountains of Greece are where you are searching (but since there is nothing there I'm guessing it's a mistake). Looks like you are looking in Saudi Arabia, so change it to:

location = '21.569649,39.169061'
0

Take note that the url params in the while statement should be as follows

  while 'next_page_token' in data:
      next_page_token = data['next_page_token']
      url = f'https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken={next_page_token}&key={api_key}'
      ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.