antfraia's picture
Update app.py
dc8e36b
raw
history blame
1.52 kB
import streamlit as st
from serpapi import GoogleSearch
def fetch_google_maps_results(query=None, location=None, search_type="search"):
if not query and not location:
raise ValueError("Either 'query' or 'location' must be provided.")
params = {
"api_key": "YOUR_API_KEY",
"engine": "google_maps",
"type": search_type,
"google_domain": "google.it",
"hl": "it",
"gl": "it"
}
if query:
params["q"] = query
if location:
params["ll"] = location
search = GoogleSearch(params)
results = search.get_dict()
return results
st.title("Google Maps API Search via SerpAPI")
st.write("Enter a search query or a geographic location to fetch results from Google Maps.")
# Input fields
search_type = st.selectbox("Select Search Type:", ["search", "place"])
query = st.text_input("Search Query (q): (Use for regular Google Maps search)")
location = st.text_input("Geographic Location (ll): (Format: '@latitude,longitude,zoom')")
# Button to trigger the API call
if st.button("Search"):
if not query and not location:
st.error("Please enter either a search query or a geographic location.")
else:
try:
results = fetch_google_maps_results(query=query, location=location, search_type=search_type)
# Display the results (you might want to format or filter this)
st.write(results)
except Exception as e:
st.error(f"Couldn't fetch the results. Error: {e}")