Skip to content

Instantly share code, notes, and snippets.

@mofax
Created January 10, 2024 12:42
Show Gist options
  • Save mofax/fb5d36ea7292a1eb39ed005880410155 to your computer and use it in GitHub Desktop.
Save mofax/fb5d36ea7292a1eb39ed005880410155 to your computer and use it in GitHub Desktop.
roads and petrol stations
import requests
# Your Google Maps API key
API_KEY = 'YOUR_API_KEY'
# The list of petrol stations you have
# Example: [{'name': 'Rubis Karen', 'latitude': -1.2921, 'longitude': 36.8219}]
petrol_stations = [...] # replace with your actual list
def get_road_name(lat, lon, api_key):
# Geocoding API endpoint
endpoint = f"https://maps.googleapis.com/maps/api/geocode/json"
# Set up parameters for the API call
params = {
'latlng': f"{lat},{lon}",
'key': api_key
}
# Make a request to the Geocoding API
response = requests.get(endpoint, params=params)
if response.status_code == 200:
results = response.json().get('results', [])
if results:
# Parsing through the results for road name (if available)
for component in results[0].get('address_components', []):
if 'route' in component.get('types', []):
return component.get('long_name')
return None
# Update the petrol stations list with the name of the road
for station in petrol_stations:
road_name = get_road_name(station['latitude'], station['longitude'], API_KEY)
station['road_name'] = road_name if road_name else "Not found"
# Print the updated list
print(petrol_stations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment