About Us
IDKBlogs

  All  
Javascript
NodeJS
AngularJS
Angular2+
ReactJS
Others

Integrate google API/MAP in your Angular2+ app

  idkblogs.com      April 23, 2018

Shubham Verma

Shubham Verma


Integrate google API/MAP in your Angular2+ app

I

n this article, we will learn how we can integrate google APIs in our angular2+ web app using 'googlemaps' module and we'll display the google map in our app. we will add CDN index.html with google API key. First we need to get the google API key, keep this google API key handy to you.

Let's start

Create an angular2+ app:

First we need to create an app using "ng new" command, let's create an app using below command:

ng new drag-and-drop
cd drag-and-drop
npm install

After successful "npm install" your angular demo app is created, Now open your terminam/CMD and locate your project root and follow the below steps:

Install googlemaps:

To use google APIs in our component, we need to install a npm modeule "@types/googlemaps". So install this module using below command:

npm install @types/googlemaps — save

The bove command will install the "googlemaps" in your app and make an entry in your package.json file.

Add CDN into your index.html with correct google API key ( Don’t forget it ):

Now we need to add the CDN link in your app's index.html file which is in the root location. Now add below CDN in your insex.html and don't forget to replace your "YOUR_GOOGLE_API_KEY" with keys.

Add below line to your html where you want to add google map:

This time we need to add the "#googleMap" in your "map.component.html" file

Now create the map.component.ts and follow the steps

Now you add below code into your angular component (map.component.ts):


                    import { Component, ViewChild } from ‘@angular/core’;
                    import { } from ‘@types/googlemaps’;
                    
                    @Component({
                      selector: ‘map- component’,
                      templateUrl: ‘./map.component.html’,
                     styleUrls: [‘./ map.component.css’]
                     }) 
                    
                     export class MapComponent {
                    
                      @ViewChild(‘googleMap’) gmapElement: any;
                    
                      map: google.maps.Map; ngOnInit() {
                    
                        var mapProp = {
                          center: new google.maps.LatLng(28.4595, 77.0266),
                          zoom: 14,
                          // mapTypeId: google.maps.MapTypeId.ROADMAP
                          mapTypeId: google.maps.MapTypeId.HYBRID
                          // mapTypeId: google.maps.MapTypeId.SATELLITE
                          // mapTypeId: google.maps.MapTypeId.TERRAIN
                        };
                    
                        this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
                        var marker = new google.maps.Marker({ position: mapProp.center });
                        marker.setMap(this.map);
                    
                        var infowindow = new google.maps.InfoWindow({
                          content: “Hey, We are here”
                        }); infowindow.open(this.map, marker);
                      }
                    }
                        

Congratulations !!! You did it.

This is the snapshot what you did.

idkblogs.com: Integrate google map in angular2+ app

Source: idkblogs.com


If you want to add multiple marker and its separate tooltip then click here to add multiple marker and its separate tooltip

Multiple marker and its separate tooltip snapshot:

idkblogs.com: Integrate google map in angular2+ app

Source: idkblogs.com

Conclusion:

In this article, we learned how we can integrate google map in our angular2+ app component and also learned how we can show the geo location in google map.


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.



OTHER POSTS

Copyright © 2020 IDKBlogs | All Right Reserved

About Us

Powered by : IDKBlogs

Last Update : 19/09/2020