pip install folium
import folium
from IPython.display import display
map_center = [40.7128, -74.0060]
mymap = folium.Map(location=map_center, zoom_start=12)
folium.Marker(
[40.7128, -74.0060],
popup="New York",
icon=folium.Icon(color="blue", icon="info-sign")
).add_to(mymap)
display(mymap)
The above code is used to create and display an interactive map using the folium library in Python, specifically within a Jupyter Notebook environment. Below is a step-by-step explanation of the code:
1. Install the folium library
pip install folium- pip install folium: This command installs the folium library, which is used for creating interactive maps with Leaflet.js, a popular JavaScript library for mapping.
2. Import necessary libraries
import foliumfrom IPython.display import display
- import folium: This imports the folium library, which allows you to create maps and add various layers and markers to them.
- from IPython.display import display: This imports the display function from IPython.display, enabling the map to be rendered directly within a Jupyter Notebook.
3. Define the center of the map
map_center = [40.7128, -74.0060]- map_center: This variable defines the latitude and longitude coordinates of the center of the map, which in this case is set to New York City (latitude: 40.7128, longitude: -74.0060).
4. Create a folium map object
mymap = folium.Map(location=map_center, zoom_start=12)- folium.Map(location=map_center, zoom_start=12): This creates a map centered at the specified location with a zoom level of 12, which gives a city-level view of New York.
5. Add a marker to the map
folium.Marker( [40.7128, -74.0060],
popup="New York",
icon=folium.Icon(color="blue", icon="info-sign")
).add_to(mymap)
- folium.Marker([40.7128, -74.0060], popup="New York",
- icon=folium.Icon(color="blue", icon="info-sign")): This adds a marker to the map at the specified coordinates (New York City). The marker has a popup label "New York" that appears when the marker is clicked, and the icon is styled with a blue color and an info-sign symbol.
- .add_to(mymap): This adds the marker to the mymap object.
6. Display the map
display(mymap) display(mymap): This displays the map in the Jupyter Notebook.
Summary
This code creates an interactive map centered on New York City, adds a marker with a popup and a custom icon, and displays the map within a Jupyter Notebook.
0 Comments:
Post a Comment