DEV Community

Lucas Pereira de Souza
Lucas Pereira de Souza

Posted on

Interactive maps with Leaflet.js

logotech

## Installation, Basic Map, Markers, and Layers: Getting Started with Interactive Maps

In this post, we will explore the essential steps to create interactive maps, starting from scratch. We will cover the installation of a popular library, the creation of a base map, and the addition of important elements such as markers and layers.

1. Installing the Library

The first step is to install the library we will use to build our maps. There are several options, but for this tutorial, we will use [Library name]. To install, open the terminal or command prompt and run the following command:

pip install [library name]
Enter fullscreen mode Exit fullscreen mode

Replace [library name] with the actual name of the library (e.g., folium, leaflet, etc.). Wait for the installation to complete.

2. Creating a Basic Map

With the library installed, we can create our base map. Let's start with a simple map, centered on a specific location. See the example code:

import [library name]

# Define the central map coordinates (latitude, longitude)
latitude = -23.5489
longitude = -46.6388

# Create the map object
mapa = [library name].Map(location=[latitude, longitude], zoom_start=12) # Adjust the zoom as needed

# Display the map (save it to an HTML file or show it in an interactive environment)
mapa.save(\"basic_map.html\") # To save to an HTML file
# or
mapa  # If in an interactive environment like a Jupyter notebook
Enter fullscreen mode Exit fullscreen mode

This code imports the library, defines the coordinates of a location, and creates a map object. The zoom_start function defines the initial zoom level. Finally, the map is displayed, either by saving it to an HTML file or showing it directly in an interactive environment.

3. Adding Markers

Markers are essential for indicating points of interest on the map. Let's add some markers to our base map:

import [library name]

# ... (base map code) ...

# Add a marker
[library name].Marker(
    location=[latitude, longitude],
    popup=\"Initial Location\", # Text that appears when you click on the marker
    tooltip=\"Click here\", # Hint that appears when you hover the mouse
).add_to(mapa)

# Add another marker
[library name].Marker(
    location=[-23.55, -46.64], # Coordinates of another point
    popup=\"Another Location\",
).add_to(mapa)

mapa.save(\"map_with_markers.html\")
# or
mapa
Enter fullscreen mode Exit fullscreen mode

This code adds two markers to the map. Each marker is created with its location, popup text, and hint (tooltip). The .add_to(mapa) method adds each marker to the map.

4. Adding Layers

Layers allow you to visualize additional information on the map, such as polygons, lines, or data from different sources. Let's add a simple layer, such as a polygon:

import [library name]

# ... (base map code) ...

# Add a polygon layer
[library name].Polygon(
    locations=[
        [-23.54, -46.63], # Coordinates of the first point
        [-23.54, -46.65], # Coordinates of the second point
        [-23.56, -46.65], # Coordinates of the third point
        [-23.56, -46.63], # Coordinates of the fourth point
    ],
    color='red', # Outline color
    fill=True, # Fill the polygon
    fill_color='red', # Fill color
    fill_opacity=0.3 # Fill opacity
).add_to(mapa)

mapa.save(\"map_with_layers.html")
# or
mapa
Enter fullscreen mode Exit fullscreen mode

This example adds a red polygon to the map. The Polygon function receives the coordinates of the vertices, defines the color and fill, and the .add_to(mapa) method adds the layer to the map.

Conclusion

In this tutorial, we learned the fundamental steps to create interactive maps. We started with the installation of the library, created a base map, added markers and layers. With these concepts, you can start exploring and customizing your own maps, adding data and functionalities to meet your needs. Remember to consult the documentation of the chosen library for more detailed information and explore other resources.

Top comments (0)