angular google maps

Step 1: Creating new project
Create a new project using command ng new gmaps-ng5
Step 2: Install Google Maps types for typescript support.
Run command npm install --save @types/googlemaps
Step 3: Link Google Maps JavaScript CDN inside index.html
<script src="http://maps.googleapis.com/maps/api/js"></script>
NOTE: 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.
With above steps, you are all set to start working with Google Maps (GMap).
Step 4: Next, let’s add a placeholder <div> for GMap
<div #gmap style="width:100%;height:400px"></div>
Step 5: Initialize GMap inside component

	import { ViewChild } from '@angular/core';
	import { } from '@types/googlemaps';

	export class AppComponent {  
	  @ViewChild('gmap') gmapElement: any;
	  map: google.maps.Map;

	  ngOnInit() {
		var mapProp = {
		  center: new google.maps.LatLng(18.5793, 73.8143),
		  zoom: 15,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
	  }
	}

Let’s break it down.
import { } from '@types/googlemaps';

First 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.
Access <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.
ngOnInit(): 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.
Running application:
Run application using ng serve and you should see Google Map inside browser. Congrats!! See, it was easy, told ya!
Perform Map operations
By 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.

Change Map type

	<div class="col-md-3 col-sm-12 col-xs-12">
	  <button (click)="setMapType('terrain')" class="btn btn-primary">Terrain</button>
	  <button (click)="setMapType('satellite')" class="btn btn-danger">Satellite</button>
	  <button (click)="setMapType('roadmap')" class="btn btn-warning">Road Map</button>
	</div>
	
	setMapType(mapTypeId: string) {
		this.map.setMapTypeId(mapTypeId)    
	}

We 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.
Navigate Map via Latitude and Longitude
Let’s create a HTML form for user to enter Latitude and Longitude.

	<form class="form-inline" #form="ngForm" (ngSubmit)="setCenter($event)" ac>
	  <div class="form-group">
		<input type="text" class="form-control" name="latitude" [(ngModel)]="latitude" placeholder="Enter latitude" required>
	  </div>
	  <div class="form-group">
		<input type="text" class="form-control" name="longitude" [(ngModel)]="longitude" placeholder="Enter longitude" required>
	  </div>
	  <button type="submit" class="btn btn-primary" [disabled]="form.invalid">Go</button>
	</form>

Once 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.
Notice e.preventDefault() function call. It is to avoid refreshing complete page on form submit.

	export class AppComponent {
	  latitude:number;
	  longitude:number;

	  setCenter(e:any){
		e.preventDefault();
		this.map.setCenter(new google.maps.LatLng(this.latitude, this.longitude));
	  }
	}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to integrate google maps in angular using google maps on angular project angular google maps link google maps on angular 10 how add google map in angular google maps angular 8 how to use google map in angular app angular google-map angular-google-maps bower how to use google maps api in angular api google maps angular google maps angular tutorial google map angular module integrate google maps angular how to implement google maps in angular Angular/google map google map component angular 9 google maps component angular google maps for angular 9 angular and google map angular googlemaps google map in angular 7 angularu google map google maps types angular Angular Google Map Module google map angular 9 google map angular 5 angular google map types google maps api in angular 8 use google maps in angular google map angular material library google maps angular google map angular 10 angularjs google maps example use google maps in angularJs google-maps angular 11 angular 12 google maps google maps pour angular 10 angular google maps - angular ui angular google mapps angular component google-maps using google maps in angular app using goole maps api in angular google maps platform with angular angular 11 google maps example integrate google maps with angular google map javascript angular angular google-maps options angular 9 google maps official library angular google maps official library integrate google maps in angular @angular/google-maps example angular google map example google maps angularjs example google maps integration in angular 9 new google.maps angular google maps angular creation google map implementation in angular google maps in angular 11 angular app google maps google map with angular google map in angular 8 angualr google maps Angular 11/10 Google Maps Example Tutorial angular googlemap implement google maps in angular 6 angular google map angularJS and google maps angular and google maps angular 10 google maps google maps angular 10 google-maps angular maps google com maps api javascript in angular google maps api in angular google maps angular 11 how to use google map in Angular google map for angular @angular/google-maps api how to use google maps in your project with angular angular 6 google maps example google-map angular angular2 google map angular g maps angular google maps tutorial angularjs google map google maps library angular 9 google maps library angular angular 8 google maps example angluar google maps using google maps in angualr angular google maps api google map for angular 11 google.maps.map angular angular js google maps google maps angualr api google maps on angular angular material google maps angular google maps javascript api google maps in angular 4 google maps for angular 10 google map with angular 10 google maps angular example google maps with angular 10 angular 11 google maps google maps for angular add google maps angular google maps angualr angular google maps example goole map with angular angular 9 google maps component integrare google maps angular google maps API angular google maps angularjs control angular new google.maps.Ma angular agm google maps agm angular 2 + component angular 9 google maps angular google maps geofence angular maps angular how to use google maps with angular gmaps angular google maps with angular angular google marker link adding google map in html angular example adding map in angular angular here maps angularjs google maps google map in angular how to embed google maps in angular embed map in angular implement google map angular how to use gmap map javascript api in Angular display google maps angular maker plot angular in goole map load google map in angular 6 using map js Angular using google map angular google maps in angular angular agm map google maps in angular agm angular google map using google maps angualr init google map in angular angular map component using map without google map in angular angular google-maps angular google maps component amngular maps angular foofl map google maps embed api angular angular maps google map angular angular maps component angular google map free ag map angular angular 5 google maps version google navigation in angular angular import googlemaps google maps angular ts angular google map @angular google maps googlemaps angular how to use google maps in angular 6 google maps angular angular google maps
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source