In this tutorial, I am going to introduce how easy it is to integrate Google Maps in Angular. I am going to show you how you can plot markers on a map, add a new marker, and store it to the local storage.
So are you exited???
Yes, let’s start with the tutorial. First, I will show how to setup the configuration for Google Maps.
First of all, visit this URL to integrate Google Maps in Angular.

Then, click on "Getting Started" button and scroll down to setting up angular2-google-maps and write dependency to package.json
Npm install angular2-google-maps - -save
Then, run npm install from command prompt. You can also get your Google Mapa API key as shown in the below.


Then, after creating a new project or choosing existing project, press on enable API to get your Maps API key.

Okay. Now, we are all set to integrate Google Maps in our project. I am just copying the key i.e. just generated and paste it to app.module.ts file as shown below.

Now, I am plotting my hometown Ahmedabad on map. To do so, I am first statically creating two variables on app.component.ts naming them as "lat" and "lng" and then, I will bind them to html, as shown in below files.
app.component.ts
- import { Component } from '@angular/core';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- lat: number = 23.022505;
- lng: number = 72.571362;
- }
app.component.html
- <div class="page-header">
- <h1>Google Map In Angular </h1>
- </div>
- <div class="container">
- <div class="row">
- <div class="col-md-12">
- <div class="map">
- <sebm-google-map [latitude]="lat" [longitude]="lng">
- <sebm-google-map-marker [latitude]="lat" [longitude]="lng"></sebm-google-map-marker>
- </sebm-google-map>
- </div>
- </div>
- </div>
- </div>

Isn’t it easy friends??? But again, it’s just a static one. Now, in next step, I will create an array of lat and lng and plot all the elements of array to map. So, let’s just do it.!!
To do it, I am first creating an interface named as marker to provide type that contains three variables- name, lat, and lng.
- export interface marker{
- name?:string;
- lat:number;
- lng:number;
- }
Now, to keep my logic centralized, I am creating one service and naming it as markerdata.service.ts and inside the service, I am creating the array of markers and methods to return the array as shown below and as you know services must be provided in order to use them in projects. (i.e. service must be declared in providers array inside app.module.ts)
Markerdata.service.ts
- import { Injectable } from '@angular/core';
- import { marker } from './marker';
- @Injectable()
- export class MarkerdataService {
- markers: marker[] = [
- {
- name: 'Ahmedabad',
- lat: 23.022505,
- lng: 72.571362
- },
- {
- name: 'Baroda',
- lat: 22.307159,
- lng: 73.181219
- },
- {
- name: 'Surat',
- lat: 21.170240,
- lng: 72.831061
- },
- {
- name: 'Rajkot',
- lat: 22.303894,
- lng: 70.802160
- },
- ];
- constructor() { }
- getAllMarkers() {
- if (localStorage.getItem('markers') === null || localStorage.getItem('markers') === undefined) {
- localStorage.setItem('markers', JSON.stringify(this.markers));
- return this.markers;
- }
- else {
- var markers = JSON.parse(localStorage.getItem('markers'));
- return markers;
- }
- }
- addMarker(newmarker: marker) {
- var markers = JSON.parse(localStorage.getItem('markers'));
- markers.push(newmarker);
- localStorage.setItem('markers', JSON.stringify(markers));
- }
- removeMarker(mark: marker) {
- var markers = JSON.parse(localStorage.getItem('markers'));
- markers.splice(markers.indexOf(mark),1);
- localStorage.setItem('markers', JSON.stringify(markers));
- }
- }



Yogesh JadhavPosted Mar 8, 2017, 5:02 AM
Module '"c:/angular2/HelloWorld/src/app/markerdata.service"' has no exported member 'MarkerDataService'.