Open In App

Angular PrimeNG GMap Overlays

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG GMap Overlays

The GMap is a google map component that is used to display a map on the webpage. We can perform zoom-in and out functions by default in the GMap component. PrimeNG also provides extra features so that we can add markers, and change the location by changing latitude and longitude and other features. 



Angular PrimeNG GMap Overlays: The various kinds of Overlays, like markers, polygons, and circles, can be rendered by the GMap Component. Overlay instances can be bound with the help of the overlays property array. Overlays are aware of one-way binding. The gmap updates itself, whenever the array changes.

Syntax:



this.overlays = [

    //For Marker
    new google.maps.Marker({
        position: { lat: 22.72105, lng: 88.373459 },
        title: 'Khardah',
    }),

    //For Circle

    new google.maps.Circle({
        center: { lat: 22.72105, lng: 88.373459 },
        fillColor: 'orange', fillOpacity: 0.35, strokeWeight: 1, radius: 1500
    })

];

Steps for setting up the Angular PrimeNG GMap: Create a GMap component as follows:

import { GMapModule } from 'primeng/gmap';
<p-gmap
    [options]="options">
</p-gmap>
ngOnInit() {
  this.options = {
    center: { lat: 22.72105, lng: 88.373459 },
    zoom: 12,
  };
}

Creating Angular application & module installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

index.html




<!doctype html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
    <title>GFG</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 async src=
    </script>
</body>
  
</html>

Example 1: In the following example, we have added overlays to the GMap component by adding markers to two landmarks.




<div style="width:80%">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG GMap Overlays
    </h2>
    <p-gmap [options]="options" 
            [overlays]="overlays" 
            [style]="{ width: '100%', 
                       height: '320px' }">
    </p-gmap>
</div>




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
  
declare var google: any;
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    constructor(private messageService: MessageService) { }
    options: any;
  
    overlays: any[] = [];
    ngOnInit() {
        this.options = {
            center: { lat: 22.72105, lng: 88.373459 },
            zoom: 12,
        };
  
        this.overlays = [
            new google.maps.Marker({
                position: { lat: 22.72105, lng: 88.373459 },
                title: 'Khardah',
            }),
            new google.maps.Marker({
                position: { lat: 22.764919, lng: 88.37082 },
                title: 'Barrackpore',
            }),
        ];
    }
}




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { HttpClientModule }
    from '@angular/common/http';
import { AppComponent } from './app.component';
import { ButtonModule } from 'primeng/button';
import { MessageService } from 'primeng/api';
import { GMapModule } from 'primeng/gmap';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        HttpClientModule,
        GMapModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [MessageService],
})
export class AppModule { }

Output:

 

Example 2: In this example, we will see a circular orange color overlay.




<div style="width:80%">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h2>
        Angular PrimeNG GMap Overlays
    </h2>
    <p-gmap [options]="options" 
            [overlays]="overlays" 
            [style]="{ width: '100%', 
                       height: '320px' }">
    </p-gmap>
</div>




import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
  
declare var google: any;
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    constructor(private messageService: MessageService) { }
    options: any;
  
    overlays: any[] = [];
    ngOnInit() {
        this.options = {
            center: { lat: 22.72105, lng: 88.373459 },
            zoom: 12,
        };
  
        this.overlays = [
            new google.maps.Circle({
                center: { lat: 22.72105, lng: 88.373459 },
                fillColor: 
                   'orange', fillOpacity: 0.35, strokeWeight: 1, radius: 1500
            }),
        ];
    }
}




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { HttpClientModule }
    from '@angular/common/http';
import { AppComponent } from './app.component';
import { ButtonModule } from 'primeng/button';
import { MessageService } from 'primeng/api';
import { GMapModule } from 'primeng/gmap';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        HttpClientModule,
        GMapModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [MessageService],
})
export class AppModule { }

Output:

 

Reference: https://primefaces.org/primeng/gmap


Article Tags :