Python Plotting Google Maps with Folium
Syntax
import folium
# Create map
map = folium.Map(location=[latitude, longitude], zoom_start=6)
# Add marker
folium.Marker(location=[latitude, longitude], popup='Marker Name', tooltip='Click here for more info').add_to(map)
# Save map
map.save('map.html')
Explanation
Folium is a Python library used for creating interactive maps. It uses the Leaflet.js library to create maps that can be zoomed, panned, and scrolled.
To create a map using Folium, we first import the library and create a new map using the folium.Map()
function. We pass the latitude and longitude coordinates of the starting location, as well as the zoom level we want for the map.
Next, we can add markers to the map using the folium.Marker()
function. We pass the latitude and longitude coordinates of the marker, as well as the popup
text that will appear when the marker is clicked, and the tooltip
text that will appear when the user hovers over the marker.
Once we have added all the desired markers to the map, we can save the map to an HTML file using the map.save()
function.
Example
import folium
# Create map
map = folium.Map(location=[38.9072, -77.0369], zoom_start=10)
# Add marker
folium.Marker(location=[38.9072, -77.0369], popup='Washington D.C.', tooltip='Click here for more info').add_to(map)
# Save map
map.save('map.html')
Output
The above code generates an interactive map showing the location of the marker "Washington D.C." at the latitude and longitude coordinates 38.9072 and -77.0369, respectively. The map can be opened in a web browser by opening the map.html
file that was saved.
Use
Folium is a useful tool for visualizing data on a map. It can be used to display points of interest, routes, or any other geographic data.
For example, if you have a dataset containing the locations of your company's stores or offices, you could use Folium to create an interactive map showing those locations and providing information about each one.
Important Points
- Folium is a Python library used for creating interactive maps.
- The
folium.Map()
function is used to create a new map. - The
folium.Marker()
function is used to add markers to the map. - Maps created with Folium can be saved to HTML files and viewed in a web browser.
Summary
Folium is a powerful Python library for creating interactive maps. By using the folium.Map()
function along with folium.Marker()
, it's possible to create useful visualizations of geographic data. With Folium, you can create maps that can be zoomed, panned, and scrolled, and saved as HTML files that can be opened in any web browser.