Skip to main content

Integrating Google map in Angular

here are the following steps to integrate map in angular(V7+) project:

Step 1. Generate Google API key

generate google map API key HERE

Step 2. Install Google Maps

install google map in your angular project by running this code

npm install — save @types/googlemaps

Step 3. Add Google API script

in index.html file add the following script into the head tag :

<script src=”https://maps.googleapis.com/maps/api/js?key=YOUR-API-KEY" type=”text/javascript”></script>

add your API Key inside of the script tag

Step 4. Add googlemaps to the types array

in tsconfig.app.json add googlemaps to the types array inside of the compilerOptions object.

“types”: [“googlemaps”]

after adding googlemaps, your code may look like this

tsconfig.json

Step 5. Add HTML element

Inside of your app.component.html add the following code:

<div #gmapContainer id=”map”></div>

Step 6. Add CSS

add some CSS to your map. In app.component.css file add the following:

#map { height: 550px; width: 100%; }

Step 7. Add typescript code

  • In your app.component.ts file your import line at the top of your file should look like this:
import { Component, AfterViewInit, ViewChild, ElementRef } from ‘@angular/core’;
  • Inside of the same file, import googlemaps and we give the app access to the DOM element we have created with ViewChild
import {} from 'googlemaps';

Where gmapContainer is the name of the map HTML element

  • create a file called index.d.ts within your src directory and add the following line
declare module 'googlemaps';
  • Now create a map variable that contains the Google Maps API inside of your app.component.ts.
map: google.maps.Map;

We are able to do this by google map types that we added to the project.

  • In the app.component.ts file add the following variables:
lat = 12.9716; lng = 77.5946;
coordinates = new google.maps.LatLng(this.lat, this.lng);
  • In the same file, create a mapOptions variable as follow:
mapOptions: google.maps.MapOptions = { center: this.coordinates, zoom: 10, };

set a zoom level as per your requirement.

  • Inside of your app.component.ts create a mapInitializer() function.
mapInitializer() {
this.map = new google.maps.Map(this.gmap.nativeElement,
this.mapOptions);
}
  • Import AfterViewInit and initialize AfterViewInit function.
export class AppComponent implements AfterViewInit {
ngAfterViewInit() {}
}
  • Inside of your ngAfterViewInit add the following line:
ngAfterViewInit() { this.mapInitializer(); }
  • Create a new variable for google map marker :
marker = new google.maps.Marker({ position: this.coordinates, map: this.map, });
  • Add the marker to the map, by adding the following line to the mapInitializer() function.
this.marker.setMap(this.map);
  • your app.component.ts code should look like this:
import {} from 'googlemaps';
export class AppComponent implements AfterViewInit {
title = 'angular-gmap';
@ViewChild('mapContainer', { static: false }) gmap: ElementRef;
map: google.maps.Map;
lat = 12.9716;
lng = 77.5946;

coordinates = new google.maps.LatLng(this.lat, this.lng);

mapOptions: google.maps.MapOptions = {
center: this.coordinates,
zoom: 10
};

marker = new google.maps.Marker({
position: this.coordinates,
map: this.map,
});

ngAfterViewInit() {
this.mapInitializer();
}

mapInitializer() {
this.map = new google.maps.Map(this.gmap.nativeElement,
this.mapOptions);
this.marker.setMap(this.map);
}
}

Step 8. Run your app

run app you can see map view in the result.

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...