showing results for - "angular google maps"
Julián
16 May 2020
1Step 1: Creating new project
2Create a new project using command ng new gmaps-ng5
3Step 2: Install Google Maps types for typescript support.
4Run command npm install --save @types/googlemaps
5Step 3: Link Google Maps JavaScript CDN inside index.html
6<script src="http://maps.googleapis.com/maps/api/js"></script>
7NOTE: Make sure you put your Google Map API Key here. You can get one from `https://developers.google.com/maps/documentation/javascript/get-api-key`. If not, after free usage, Google Map will start showing watermark.
8With above steps, you are all set to start working with Google Maps (GMap).
9Step 4: Next, let’s add a placeholder <div> for GMap
10<div #gmap style="width:100%;height:400px"></div>
11Step 5: Initialize GMap inside component
12
13	import { ViewChild } from '@angular/core';
14	import { } from '@types/googlemaps';
15
16	export class AppComponent {  
17	  @ViewChild('gmap') gmapElement: any;
18	  map: google.maps.Map;
19
20	  ngOnInit() {
21		var mapProp = {
22		  center: new google.maps.LatLng(18.5793, 73.8143),
23		  zoom: 15,
24		  mapTypeId: google.maps.MapTypeId.ROADMAP
25		};
26		this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
27	  }
28	}
29
30Let’s break it down.
31import { } from '@types/googlemaps';
32
33First we shall import Google Maps types that we have installed in step 2. This is great help during development as you can work with strong types instead of vague any type. Apart from that, you shall also get intellisense if your IDE can understand type definition files.
34Access <div #gmap>: gmapElement is a reference to <div #gmap> inside app.component.html file. ViewChild directive creates a direct link between <div> element and a gmapElement member variable.
35ngOnInit(): Inside ngOnInit() life cycle hook, we shall create configuration object for GMap specifying default center, zoom level and map type. We shall pass this object to google.maps.Map constructor which shall return new Map object which we shall retain in member variable map for later access.
36Running application:
37Run application using ng serve and you should see Google Map inside browser. Congrats!! See, it was easy, told ya!
38Perform Map operations
39By default Google Map control shall render map as well as have few controls for changing zoom, full screen etc. You can access native Google Maps API via Angular.
40
41Change Map type
42
43	<div class="col-md-3 col-sm-12 col-xs-12">
44	  <button (click)="setMapType('terrain')" class="btn btn-primary">Terrain</button>
45	  <button (click)="setMapType('satellite')" class="btn btn-danger">Satellite</button>
46	  <button (click)="setMapType('roadmap')" class="btn btn-warning">Road Map</button>
47	</div>
48	
49	setMapType(mapTypeId: string) {
50		this.map.setMapTypeId(mapTypeId)    
51	}
52
53We have map member variable inside our AppComponent class. Using this variable, we can call native GMap API for example, setMapTypeId. We have three buttons which pass map type ID to click handler to be passed to setMapTypeId function.
54Navigate Map via Latitude and Longitude
55Let’s create a HTML form for user to enter Latitude and Longitude.
56
57	<form class="form-inline" #form="ngForm" (ngSubmit)="setCenter($event)" ac>
58	  <div class="form-group">
59		<input type="text" class="form-control" name="latitude" [(ngModel)]="latitude" placeholder="Enter latitude" required>
60	  </div>
61	  <div class="form-group">
62		<input type="text" class="form-control" name="longitude" [(ngModel)]="longitude" placeholder="Enter longitude" required>
63	  </div>
64	  <button type="submit" class="btn btn-primary" [disabled]="form.invalid">Go</button>
65	</form>
66
67Once user submit the form by entering Latitude and Longitude, we shall call native setCenter GMap API. This function takes object of LatLng type hence we shall pass lat/long as a parameter to LatLng constructor.
68Notice e.preventDefault() function call. It is to avoid refreshing complete page on form submit.
69
70	export class AppComponent {
71	  latitude:number;
72	  longitude:number;
73
74	  setCenter(e:any){
75		e.preventDefault();
76		this.map.setCenter(new google.maps.LatLng(this.latitude, this.longitude));
77	  }
78	}
queries leading to this page
use google map in angular 9angular here mapsintegrate google maps with angular 11google maps types angularusing google maps with angulargoogle map component angularusing google maps in angularembed map in angularagm angular google mapinit google map in angularangular 5 google mapsgoogle maps agm angular 2 2b componentangular and google mapangular foofl mapangular material google maps google maps module angularimplement google map angulargoogle map angular 10google map in angular 8angular google map freegoogle maps angular examplegoogle maps on angularangular google maps exampleangular 8 google maps exampleangular new google maps maangular google maps angular uigoogle maps angular tsangular1 google mapintegrate google maps angularintegrare google maps angularimplement google maps in angular 6angluar google mapsusing google maps in angualrangular googlemap google maps implementation angularmaps google com maps api javascript in angulardisplay google maps angulargoogle maps angular 11how to use the google maps in angular 9google maps angular docsgoogle maps angularangular google mapofficial angular google maps componentangular google maps version for angular 9use google maps in angulargoogle maps platform with angularusing goole maps api in angularangular g maps how to use google maps with angular 40angular google mapsusing google maps angualrgoogle maps in angular 4how to use google maps in your project with angularangularjs google mapangular 2fgoogle maps usage angular 5angular google map moduleusing google maps with angulaangular and google mapsgmaps angulargoogle maps on angular 10google map angularhow to implement google maps in angularangular component google mapsgoogle maps for angular 11angular 11 google maps examplegoogle maps for angular 10angular google maps docsangular 12 google js mapsjs google map api angularhow to embed google maps in angularangular google maps integrationangular google maps mapangular google mapangular 2fgoogle maps for angular 11angular google maps documentationgoogle maps for angularhow to use google maps api in angularintegrate google maps with angularintegrate google maps in angularangular google maps javascript apiadd google maps angulargoogle maps with angular 10angular 5 google mapangular 9 google mapsgoogle map with js angulargoogle maps with angularmaker plot angular in goole map google maps in angularangularjs google maps exampleangular google map typesangular using google mapgoogle maps api angularangular 11 2f10 google maps example tutorialapi google maps angular how to integrate google maps in angular 40angular 2fgoogle maps apihow to use gmap map javascript api in angular 40angular 2fgoogle maps examplegoogle map for angularangular map componentangular google maps linkgoogle map angulargoogle map in angularangular google mapsangular agmgoogle map with angular 10google maps javascript angularangularjs example google maphow add google map in angulargoogle map in angular 10javascript google map with angularjsgoogle maps pour angular 10google maps integration in angular 9google map in angularangularu google mapangular google maps geofenceangular2 google mapuse google maps in angularjsusing map without google map in angularangular google maps tutorialgoogle maps angular componentgoogle maps in angular 11angular googlemapsangular 12 google mapsangular google maps plugingoogle maps angular tutorialgoogle maps angularjs controlgoogle maps angular 12google maps angular creationgoogle map with angulargoogle maps angular 11google maps angualrangular new google maps mapimplement google maps in angularangular google maps in angularangular 11 google mapsgoogle map in angular 11googlemaps angularload google map in angular 6 using map jsgoogle maps angular 8google maps api in angular 8angular import googlemapsgoogle navigation in angularangular google maps optionsgoogle maps angular libraryangular google maps official libraryadd google maps api angulargooglemap with angulargoogle maps docs for angularangular js google mapsangular 9 google maps componentadding map in angularamngular mapsangular google mapsgoogle maps types angularmake an angular app that uses google mapsangular google map exampleadding google map in html angular examplehow to use google maps in angular 10angular 10 google maps examplegoogle maps angulargoogle map javascript angularangualr google mapsgoogle maps angualr apigoole map with angulargoogle maps embed api angular google maps map angulargoogle maps in component 7dangulargoogle map component from angulargoogle map angular 9google maps api in angulargoogle maps library angular 9google map angular 5using google maps with angular 11google map angular moduleangular maps componentangularjs and google mapsangular mapsgoogle map implementation in angularangular agm mapgoogle map in angular 7angular 9 google maps official libraryuse google map with angularhow to use google map in angulargoogle maps in angular 12angular 2fgoogle mapusing google maps in angular appgoogle maps javascript api angularnew google maps angulargoogle map angular materialangular google marker linkgoogle maps library angularangular google maps apiangular 10 google mapsusing google maps on angular projectangularjs google mapsgoogle maps angularjs examplegoogle map component angular 9angular app google mapsangular google maps componentgoogle map angular 11google maps for angular 9angular google maps bowergoogle maps component angularangular google mappsgoogle maps angular 10how to use google maps in angular 6angular google maps angular 12 40angular 2fgoogle maps documentationgoogle map in angular 12how to use google map in angular appag map angularlibrary google maps angularangular 5 google maps versionangular 6 google maps examplegoogle maps in angularintegrate google maps with angular 11angular maps angulargoogle map for angular 11google maps angularuse google maps angulargoogle maps in angular routingangular google mapangular google maps