angular google maps
JavaScript
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));
}
}
Also in JavaScript:
- Title
- how to check if object has key javascript
- Category
- JavaScript
- Title
- how to get today date in javascript
- Category
- JavaScript
- Title
- ad data to js object
- Category
- JavaScript
- Title
- are you sure javascript
- Category
- JavaScript
- Title
- get url params angular
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- iterate object js
- Category
- JavaScript
- Title
- async await javascript
- Category
- JavaScript
- Title
- iterate array in javascrpt
- Category
- JavaScript
- Title
- how to filter object in javascript
- Category
- JavaScript
- Title
- how to make a show password button
- Category
- JavaScript
- Title
- dropzone add download button addedfile
- Category
- JavaScript
- Title
- bind and unbind jquery validation
- Category
- JavaScript
- Title
- how to get element from arraylist react
- Category
- JavaScript
- Title
- autocomplete list angular 8 material
- Category
- JavaScript
- Title
- how to append select option in jquery
- Category
- JavaScript
- Title
- display none after hover
- Category
- JavaScript
- Title
- delegate jquery
- Category
- JavaScript
- Title
- flask get summernote text
- Category
- JavaScript
- Title
- firebase ci cd angular
- Category
- JavaScript
- Title
- how to attach javascript to html
- Category
- JavaScript
- Title
- compare two arrays javascript
- Category
- JavaScript
- Title
- Add table row in jQuery
- Category
- JavaScript
- Title
- edje js not pre compile for node 12 version
- Category
- JavaScript
- Title
- angular google maps
- Category
- JavaScript
- Title
- how to find id in array javascript
- Category
- JavaScript
- Title
- ERROR in ./src/app/pages/auth/auth.module.ngfactory.js Module not found: Error: Can't resolve '@angular/material/core/index'
- Category
- JavaScript
- Title
- fill array with values javascript
- Category
- JavaScript
- Title
- create react component class
- Category
- JavaScript
- Title
- change width in js
- Category
- JavaScript
- Title
- box shadow javascript style change
- Category
- JavaScript
- Title
- append to array js
- Category
- JavaScript
- Title
- choose random from array javascript
- Category
- JavaScript
- Title
- angular property binding
- Category
- JavaScript
- Title
- document addeventlistener click
- Category
- JavaScript
- Title
- chrome add bookmark that prefixes text
- Category
- JavaScript
- Title
- call javascript function from html
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- expo open app settings
- Category
- JavaScript
- Title
- inline z-index react
- Category
- JavaScript
- Title
- clear input field value javascript
- Category
- JavaScript
- Title
- discord.js
- Category
- JavaScript
- Title
- filter advantages in js
- Category
- JavaScript
- Title
- how to create a popup in javascript
- Category
- JavaScript
- Title
- how to create duplicate key array in javascript
- Category
- JavaScript
- Title
- discord.js send message to channel
- Category
- JavaScript
- Title
- adding element to array javascript
- Category
- JavaScript
- Title
- angular http client
- Category
- JavaScript
- Title
- how to run p5js in a particular section of page
- Category
- JavaScript
- Title
- bash parse json
- Category
- JavaScript
- Title
- Javascript detect mobile browser
- Category
- JavaScript
- Title
- convert date to string javascript
- Category
- JavaScript
- Title
- How do you call a functional component in React
- Category
- JavaScript
- Title
- array.unshift in javascript
- Category
- JavaScript
- Title
- how to use jQuery hide and show to display multiple images
- Category
- JavaScript
- Title
- callback without duplicates javascript
- Category
- JavaScript
- Title
- how to load localt ext file in js
- Category
- JavaScript
- Title
- how to include local image files in javascript object
- Category
- JavaScript
- Title
- javascript array of objects access properties
- Category
- JavaScript
- Title
- hide urls in .env in react app
- Category
- JavaScript
- Title
- array.filter in js
- Category
- JavaScript
- Title
- how to call action from another module vuex
- Category
- JavaScript
- Title
- how to update kali linux on virtualbox
- Category
- JavaScript
- Title
- how to compare strings javascript
- Category
- JavaScript
- Title
- convert jquery to string
- Category
- JavaScript
- Title
- how to add object to array javascript
- Category
- JavaScript
- Title
- convert json to csv npm
- Category
- JavaScript
- Title
- defineCall is not a function at Sequelize.import sequelize.js
- Category
- JavaScript
- Title
- ckeditor get instance from textarea
- Category
- JavaScript
- Title
- console.log javascript
- Category
- JavaScript
- Title
- error Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\].*|website\\node_modules\\.*|heapCapture\\bundle\.js|.*\\__tests__\\.*)$/: Unterminated character class.
- Category
- JavaScript
- Title
- add char in specific index stirng javascript
- Category
- JavaScript
- Title
- array vowels js
- Category
- JavaScript
- Title
- auto refresh page javascript
- Category
- JavaScript
- Title
- fetch a json from url python
- Category
- JavaScript
- Title
- chart.js radar chart
- Category
- JavaScript
- Title
- drupal 8 check if current page is node
- Category
- JavaScript
- Title
- crypto.com api
- Category
- JavaScript
- Title
- add select option jquery
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- get selected option value
- Category
- JavaScript
- Title
- arrow function javascript ie11
- Category
- JavaScript
- Title
- go to page jquery
- Category
- JavaScript
- Title
- Array Helpers
- Category
- JavaScript
- Title
- async reduce javascript
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- how to make a vowel counter in javascript
- Category
- JavaScript
- Title
- how to make background image move mmousemove jquery
- Category
- JavaScript
- Title
- adding a variable to a string without using + in javascript
- Category
- JavaScript
- Title
- git empty commit
- Category
- JavaScript
- Title
- if object is array javascript
- Category
- JavaScript
- Title
- how to move a block in javascript
- Category
- JavaScript
- Title
- contains substring javascript
- Category
- JavaScript
- Title
- funcao com o botao esc do teclado javascript
- Category
- JavaScript
- Title
- call javascript function from php
- Category
- JavaScript
- Title
- Encrypt in PHP openssl and decrypt in javascript CryptoJS
- Category
- JavaScript
- Title
- anjular js
- Category
- JavaScript
- Title
- comparing arrays javascript
- Category
- JavaScript
- Title
- javascript array find highest value of array of objects by key
- Category
- JavaScript
- Title
- flip a coin javascript
- Category
- JavaScript