DEV Community

Cover image for Interactive Map with Angular CLI
Akash Bais
Akash Bais

Posted on

Interactive Map with Angular CLI

Building an Interactive Map with Angular CLI

Interactive maps are powerful tools for visualizing data and creating engaging user experiences. With Angular CLI, the process of integrating maps into your web applications becomes streamlined and efficient. In this tutorial, we will walk through the steps to create an interactive map using Angular CLI and the popular Leaflet library.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

  • Node.js and npm
  • Angular CLI

You can install Angular CLI globally using npm if you haven't already:

npm install -g @angular/cli
Enter fullscreen mode Exit fullscreen mode

Setting Up a New Angular Project

Let's start by creating a new Angular project. Open your terminal and run the following commands:

ng new interactive-map-app
cd interactive-map-app
Enter fullscreen mode Exit fullscreen mode

Installing Leaflet

Next, we need to install Leaflet and its Angular wrapper. Leaflet is a lightweight open-source library for interactive maps.

npm install leaflet @asymmetrik/ngx-leaflet
Enter fullscreen mode Exit fullscreen mode

Creating the Map Component

Now, let's generate a new Angular component to hold our map. Run the following command:

ng generate component map
Enter fullscreen mode Exit fullscreen mode

This will create a new folder map inside the src/app directory with the necessary files.

Implementing the Map Component

Open the map.component.ts file and import the necessary modules:

import { Component, OnInit } from '@angular/core';
import { latLng, MapOptions, tileLayer, Map } from 'leaflet';
import 'leaflet/dist/images/marker-icon-2x.png';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
  mapOptions: MapOptions;
  map: Map;

  constructor() { }

  ngOnInit(): void {
    this.initializeMap();
  }

  private initializeMap() {
    this.mapOptions = {
      center: latLng(51.505, -0.09),
      zoom: 13,
    };

    this.map = new Map('map', this.mapOptions);

    tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© OpenStreetMap contributors'
    }).addTo(this.map);
  }
}
Enter fullscreen mode Exit fullscreen mode

In this code, we are defining the basic structure of our map component. We have an initializeMap method that sets the initial options for our map, creates a new Leaflet map instance, and adds a tile layer from OpenStreetMap.

Updating the Template

Next, open the map.component.html file and add the following code:

<div id="map" style="height: 400px;"></div>
Enter fullscreen mode Exit fullscreen mode

This code creates a div element with the id map where our map will be rendered. We set a height of 400 pixels, but you can adjust this according to your needs.

Styling the Map

To ensure the map displays correctly, open the map.component.css file and add the following CSS:

#map {
  height: 100%;
  width: 100%;
}
Enter fullscreen mode Exit fullscreen mode

This CSS ensures that the map takes up the entire space of its container.

Using the Map Component

Now that we have our map component set up, we can use it in our app.component.html file:

<div style="text-align: center;">
  <h1>
    Welcome to Interactive Map App!
  </h1>
</div>
<app-map></app-map>
Enter fullscreen mode Exit fullscreen mode

Running the Application

Finally, let's run our Angular application to see the interactive map in action:

ng serve
Enter fullscreen mode Exit fullscreen mode

Open your browser and navigate to http://localhost:4200. You should see the welcome message along with the interactive map rendered on the page.

Conclusion

In this tutorial, we have learned how to create an interactive map using Angular CLI and the Leaflet library. You can further enhance this map by adding markers, popups, custom tiles, and integrating with external data sources. Explore the Leaflet documentation for more advanced features and customization options.

Feel free to experiment with different map styles, layers, and interactions to create a map that suits your project requirements. Happy mapping!


Top comments (0)