Angular Google Maps API

How to integrate Google Maps API into your Angular application without fear of third-party libraries? Not many Angular developers know about the most reliable way to do this and keep using the @agm/core npm package.
AGM is a great package, but I just want to show you the alternative that is developed by the Angular team. They provided official components for Angular projects such as @angular/google-maps, @angular/youtube-player, @angular/material, etc.

So what can be better than official documentation? Let’s take a closer look at @angular/google-maps. It is the Angular component built on top of the Google Maps JavaScript API and it covers the most cases you need.
Agenda for this article:
- Creating a Google map
- Adding circles
- Adding markers and info windows
- Adding a search Input
Installation
ng add @angular/google-mapsLoading the API
Include the Dynamic Library Import script in the index.html of your app. When a Google Map is being rendered, it'll use the Dynamic Import API to load the necessary JavaScript automatically.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularGoogleMaps</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
v: "weekly",
key: 'YOUR-KEY-HERE'
});
</script>
</body>
</html>If you want to personalize your key for different users and load it from a database for example you can find out how to do this in the next article:
Creating a Google Map
Let's generate a new component for our map:
cd src/app
ng g c angular-google-mapsAdd and import Google Maps component with options:
<!-- angular-google-maps.component.html -->
<google-map [options]="options" />// angular-google-maps.component.ts
import { Component } from '@angular/core';
import {GoogleMap} from "@angular/google-maps";
@Component({
selector: 'app-angular-google-maps',
standalone: true,
imports: [
GoogleMap
],
templateUrl: './angular-google-maps.component.html',
styleUrl: './angular-google-maps.component.scss'
})
export class AngularGoogleMapsComponent {
options: google.maps.MapOptions = {
center: {lat: 40, lng: -20},
zoom: 4
};
}This will create a basic Google Map centered at coordinates (40, -20) with a zoom level of 4. If you do these steps you achieve the following result:

Adding Circles
Circles are a great way to visualize data on a map. To add a circle to your map import the MapCircle component and create an array of circles:
// angular-google-maps.component.ts
import { Component } from '@angular/core';
import {GoogleMap, MapCircle} from "@angular/google-maps";
@Component({
selector: 'app-angular-google-maps',
standalone: true,
imports: [
GoogleMap,
MapCircle
],
templateUrl: './angular-google-maps.component.html',
styleUrl: './angular-google-maps.component.scss'
})
export class AngularGoogleMapsComponent {
options: google.maps.MapOptions = {
center: {lat: 40, lng: -80},
zoom: 4
};
circles: IMapCircle[] = [
{
id: 1,
lat: 40,
lng: -80,
circleOptions: {
fillColor: '#ff0000',
fillOpacity: 0.5,
strokeWeight: 1,
strokeColor: '#ff0000',
clickable: false,
editable: false,
zIndex: 1,
radius: 100000 // in meters
}
}
]
}
interface IMapCircleOptions{
fillColor: string;
fillOpacity: number;
strokeWeight: number;
strokeColor: string;
clickable: boolean;
editable: boolean;
zIndex: number;
radius: number;
}
export interface IMapCircle{
id: number;
lat: number;
lng: number;
circleOptions: IMapCircleOptions;
}Add the circles to your map template:
<!-- angular-google-maps.component.html -->
<google-map [options]="options">
@for (circle of circles; track circle.id){
<map-circle [options]="circle.circleOptions"
[center]="circle"
/>
}
</google-map>
Now you may ask that you want to add a circle by clicking. It’s easy too. Make some changes to our code and add mapClickevent:
<!-- angular-google-maps.component.html -->
<google-map [options]="options" (mapClick)="clickMap($event)">
@for (circle of circles; track circle.id){
<map-circle [options]="circle.circleOptions"
[center]="circle"
/>
}
</google-map>Define clickMap() function:
// angular-google-maps.component.ts
clickMap(event: google.maps.MapMouseEvent) {
const lat = event.latLng?.lat();
const lng = event.latLng?.lng();
this.circles.push({
id: this.circles.length + 1,
lat: lat,
lng: lng,
circleOptions: {
fillColor: '#ff0000',
fillOpacity: 0.5,
strokeWeight: 1,
strokeColor: '#ff0000',
clickable: false,
editable: false,
zIndex: 1,
radius: 100000 // in meters
}
} as IMapCircle);
}Adding markers and info windows
Firstly, we need to import MapMarker and MapInfoWindow :
@Component({
selector: 'app-angular-google-maps',
standalone: true,
imports: [
GoogleMap,
MapCircle,
MapMarker,
MapInfoWindow
],
templateUrl: './angular-google-maps.component.html',
styleUrl: './angular-google-maps.component.scss'
})Next, we have to add markerOptions and markerWindowInfo to the circles:
// angular-google-maps.component.ts
circles: IMapCircle[] = [
{
id: 1,
lat: 40,
lng: -80,
circleOptions: {
fillColor: '#ff0000',
fillOpacity: 0.5,
strokeWeight: 1,
strokeColor: '#ff0000',
clickable: false,
editable: false,
zIndex: 1,
radius: 100000 // in meters
},
markerOptions: {
lat: 40,
lng: -80,
label: {
text: 'A',
className: 'circle-label',
fontSize: '12px'
}
},
markerWindowInfo: {
html: '<div>Marker A</div>'
}
}
]Add @ViewChildren for info windows:
// angular-google-maps.component.ts
@ViewChildren(MapInfoWindow) infoWindows!: QueryList<MapInfoWindow>;Define the function to open an info box:
// angular-google-maps.component.ts
openMarkerInfoWindow(marker: MapMarker, markerIndex: number) {
this.infoWindows.forEach((infoWindow: MapInfoWindow, index: number ) => {
if (index === markerIndex) {
infoWindow.open(marker);
} else {
infoWindow.close();
}
});
}Change HTML component:
<!-- angular-google-maps.component.html -->
<google-map [options]="options" (mapClick)="clickMap($event)">
@for (circle of circles; track circle.id){
<map-circle [options]="circle.circleOptions"
[center]="circle"
/>
<map-marker #marker="mapMarker" [position]="circle.markerOptions" [options]="circle.markerOptions" (mapClick)="openMarkerInfoWindow(marker, index)" />
<map-info-window>
<div [innerHTML]="circle.markerWindowInfo.html"></div>
</map-info-window>
}
</google-map>
Here is what it should be like:

Search Input
If you want to add search input to the map you can find the solution in Kevin Kreuzer article:
And there we have it. You can find the full source code on my GitHub page. I strongly recommend looking at the official documentation for additional features.
Thank you for reading. If you have any questions you can find me on LinkedIn. Feel free to leave comments. You can also read my previous articles: