
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Get a google map image of specified location using Google Static Maps API in Python
Google provides a static maps API that returns a map image on our HTTP request. We can directly request for a map image with different parameters based on our need.
We have to create a billing account on Google to use this API. You can go to the website for more details.
Let's see the steps to get the image of a location.
Import the requests module.
Initialise your API Key and base URL ("https://maps.googleapis.com/maps/api/staticmap?").
Initialize the city and zoom value.
Update the URL with API Key, City, and Zoom value.
USend an HTTP request. And write the response to a file for saving the image.pdate the URL with API Key, City, and Zoom value.
Example
Let's convert the above steps to code.
# importing the module import requests # base URL BASE_URL = "https://maps.googleapis.com/maps/api/staticmap?" # API key API_KEY = "Your API Key" # city CITY = "Hyderabad" # zoom value ZOOM = 14 # updating the URL URL = BASE_URL + "center=" + CITY + "&zoom=" + str(ZOOM) + "&size = 500x500&key=" + API_KEY # HTTP request response = requests.get(URL) # storing the response in a file (image) with open('hyderabad.png', 'wb') as file: # writing data into the file file.write(response.content) # make sure you have a valid API Key # You will get 403 as status_code if your API Key is invalid
Output
We will get image as below if the HTTP request is success.
Conclusion
If you have any doubts in the tutorial, mention them in the comment section.
- Related Articles
- Why Google embedded Map Makers inside Google Maps?
- Python script to open a Google Map location on clipboard?
- How to use HTML5 GeoLocation API with Google Maps?
- Google Maps Alternatives
- Speech Recognition in Python using Google Speech API
- Plotting Google Map using gmplot package in Python?
- How to show current location on a Google Map on Android using Kotlin?
- How to add Google map inside html page without using API key?
- How to show current location on a google map on Android?
- Calculate geographic coordinates of places using google geocoding API in Python?
- Plotting Google Map using folium package?
- Audio processing using Pydub and Google Speech Recognition API in Python
- How to Add Google Maps to a Website?
- Plotting Data on Google Map using pygmaps package?
- Effects and animations with Google Maps markers in HTML5
