Google Maps Markers for Angular

Google Maps provides powerful mapping technology that you can use in your own applications. In this blog, I’ll explain how you can get started using markers in Google Maps in your Angular web applications.

Introduction

In one of the previous posts, we learned how to integrate Google Maps into an Angular web application.

In this post, I will show you how to add markers to your map and how to configure each marker with positioning, use a custom icon for your markers, and a few other useful tricks.

There are two options for adding a Marker to the map:
1. Adding a Marker using <map-marker></map-marker> tag in HTML
2. Adding a Marker using the new google.maps.Marker() method in JavaScript

Preparation of Data for Google Maps Markers

The marker has a set of properties that are used to display and process Output events to the Marker on the Map.

The full reference to the API can be found in the Google Maps API Docs and the Angular implementation in the source code.

To begin with, we will create an interface for our Markers (in the future we will update this interface to add new options to the Marker), with one option position:

import { Component, ViewChild } from '@angular/core';
import { GoogleMap } from '@angular/google-maps';

interface MarkerProperties {
  position: {
    lat: number;
    lng: number;
  }
};

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  @ViewChild('myGoogleMap', { static: true }) map!: GoogleMap;

  mapOptions: google.maps.MapOptions = {
    center: { lat: 48.8588548, lng: 2.347035 },
    zoom: 13,
  };
}

After that, we will create an array of markers, I added the positions of several markers:

import { Component, ViewChild } from '@angular/core';

interface MarkerProperties {
  position: {
    lat: number;
    lng: number;
  }
};

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  mapOptions: google.maps.MapOptions = {
    center: { lat: 48.8588548, lng: 2.347035 },
    zoom: 13,
  };

  markers: MarkerProperties[] = [
    { position: { lat: 48.8584, lng: 2.2945 }}, // Eiffel Tower
    { position: { lat: 48.8606, lng: 2.3376 }}, // Louvre Museum
    { position: { lat: 48.8530, lng: 2.3499 }}, // Cathédrale Notre-Dame de Paris
  ];
}

Adding a Marker Using the MapMarker Component

Using the prepared marker interface, in the HTML file of the component, we will add ngFor to the <map-marker> tag and update our template:

<div class="map">
  <google-map height="100vh"
              width="100%"
              [options]="mapOptions">
    <map-marker *ngFor="let marker of markers"
                [position]="marker.position"></map-marker>
  </google-map>
</div>
Google Maps displaying 3 Markers added using tags

Adding a Marker Using the new google.maps.Marker() Method

To use the new google.maps.Marker() method to add a Marker to the map, we will update our component:

In the HTML file, you need to remove the tag and add an outgoing event handler for the tag.

<div class="map">
  <google-map height="100vh"
              width="100%"
              [options]="mapOptions"
              (mapInitialized)="handleMapInitialized($event)">
  </google-map>
</div>

mapInitialized event – an outgoing event that signals that the map is ready for work

readonly mapInitialized: EventEmitter<google.maps.Map>;

In the component, you need to add a handler for mapInitialized, the <google-map> output event

import { Component } from '@angular/core';

interface MarkerProperties {
  position: {
    lat: number;
    lng: number;
  }
};

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  mapOptions: google.maps.MapOptions = {
    center: { lat: 48.8588548, lng: 2.347035 },
    zoom: 13,
    mapTypeControl: false,
  };

  markers: MarkerProperties[] = [
    { position: { lat: 48.8584, lng: 2.2945 }}, // Eiffel Tower
    { position: { lat: 48.8606, lng: 2.3376 }}, // Louvre Museum
    { position: { lat: 48.8530, lng: 2.3499 }}, // Cathédrale Notre-Dame de Paris
  ];

  handleMapInitialized(map: google.maps.Map) {}
}

After adding the method to the component, let’s move on to adding Markers. We can add functionality for creating Markers to the map’s output event processing method like this:

handleMapInitialized(map: google.maps.Map) {
  this.markers.forEach((marker: MarkerProperties) => {
    new google.maps.Marker({
      position: marker.position,
      map,
    });
  });
}
Google Maps display 3 Markers added using event

Conclusion

I hope this quick guide to using Google Maps markers will help you easily get started using Google Maps markers in your Angular application. As we can see from the results, these two approaches work equally well, so the choice is yours which approach is best for your needs.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Google Maps for Angular

Google Maps provides powerful mapping technology that is available to use inside your own applications. In this blog, I'll explain how you can get started using it inside an Angular web app. Before You Start Before you can start working wit

Read More

Google Maps in Angular 20 with the JS API Loader

In this guide, we’ll walk you through how to easily integrate Google Maps into your Angular 20 application using the Google Maps JavaScript API Loader. This method allows for dynamic loading of the Maps API, keeping your bundle size lean, e

Read More

Solving Memory Leaks with Google Maps in Angular

Google Maps is a powerful tool for any application that needs location-based functionality. However, integrating it into an Angular application can sometimes lead to performance challenges. For example, in one of our projects that use Googl

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.