Angular Bootstrap Google Maps
Angular Google Maps - Bootstrap 4 & Material Design
Note: This documentation is for an older version of Bootstrap (v.4). A
newer version is available for Bootstrap 5. We recommend migrating to the latest version of our product - Material Design for
Bootstrap 5.
Go to docs v.5
As of July 16, 2018, an API Key is required to obtain this Google Maps feature. Click here to get an API Key
Angular Bootstrap Google Maps are the most advanced online maps in the world. Their uses are practically limitless.
They are mostly used on contact and about pages to increase the awareness of your location.
It's quite easy to implement a Google map in your project since all it requires is a small piece of JS code.
Examples of Angular Bootstrap Google Maps use include:
- A travelling blog with map of every journey
- Contact page
- An about section with your location
See the following Angular Bootstrap Google maps examples:
Basic Example
<agm-map [latitude]="map.lat" [longitude]="map.lng" style="height: 300px">
<agm-marker [latitude]="map.lat" [longitude]="map.lng"></agm-marker>
</agm-map>
import { Component } from '@angular/core';
@Component({
selector: 'map-example',
templateUrl: 'map.html',
})
export class MapComponent {
map: any = { lat: 51.678418, lng: 7.809007 };
}
Dynamically adding markers on the map
<agm-map [latitude]="latitude" [longitude]="longitude" style="height: 300px" (mapClick)="placeMarker($event)">
<agm-marker *ngFor="let marker of markers" [latitude]="marker.latitude" [longitude]="marker.longitude"></agm-marker>
</agm-map>
import { Component } from '@angular/core';
@Component({
selector: 'dynamic-markers',
templateUrl: './dynamic-markers.component.html',
styleUrls: ['./dynamic-markers.component.scss'],
})
export class DynamicMarkersComponent {
longitude = 20.728218;
latitude = 52.128973;
markers = [
{ latitude: 52.228973, longitude: 20.728218 }
];
placeMarker(position: any) {
const lat = position.coords.lat;
const lng = position.coords.lng;
this.markers.push({ latitude: lat, longitude: lng });
}
}
Tooltip with agm-info-window
<agm-map [latitude]="latitude" [longitude]="longitude" style="height: 300px" (mapClick)="placeMarker($event)">
<agm-marker (mouseOver)="window.open()" (mouseOut)="window.close()" *ngFor="let marker of markers" [latitude]="marker.latitude"
[longitude]="marker.longitude">
<agm-info-window #window>
<h6>Longitude: {{marker.longitude}}</h6>
<h6>Latitude: {{marker.latitude}}</h6>
</agm-info-window>
</agm-marker>
</agm-map>
import { Component } from '@angular/core';
@Component({
selector: 'agm-info-window',
templateUrl: './agm-info-window.component.html',
styleUrls: ['./agm-info-window.component.scss'],
})
export class AgmInfoWindowComponent {
longitude = 20.728218;
latitude = 52.128973;
markers = [
{ latitude: 52.228973, longitude: 20.728218 }
];
placeMarker(position: any) {
const lat = position.coords.lat;
const lng = position.coords.lng;
this.markers.push({ latitude: lat, longitude: lng });
}
}
Map types
The input [mapTypeId]
is responsible for the change of the map type and takes the values of the string
type.
Possible types are: roadmap (default), satellite, hybrid, terrain.
<agm-map [latitude]="latitude" [longitude]="longitude" style="height: 300px">
<agm-marker [latitude]="latitude" [longitude]="longitude" [mapTypeId]="'hybrid'"></agm-marker>
</agm-map>
import { Component } from '@angular/core';
@Component({
selector: 'map-types',
templateUrl: './map-types.component.html',
styleUrls: ['./map-types.component.scss'],
})
export class MapTypesComponent {
longitude = 20.728218;
latitude = 52.128973;
}
Searching for a location
The following example shows you how to search for a location on the map.
<div class="container">
<div class="row my-5 d-flex flex-center">
<div class="col-md-6">
<agm-map #map [mapTypeId]="'terrain'" [latitude]="location.lat" [longitude]="location.lng" style="height: 300px">
<agm-marker [latitude]="location.lat" [longitude]="location.lng"></agm-marker>
</agm-map>
</div>
<div class="col-md-6">
<div class="md-form">
<input type="text" [(ngModel)]='location.address_level_1' class="form-control" id="searchField"
mdbInput>
<label for="searchField">Search location</label>
</div>
<div class="row">
<div class="col-md-12 d-flex flex-center">
<button mdbBtn color="primary" mdbWavesLight (click)='updateOnMap()' type="submit">Search</button>
</div>
</div>
</div>
</div>
</div>
import { Component, OnInit, ViewChild } from '@angular/core';
import { MapsAPILoader, AgmMap } from '@agm/core';
declare var google: any;
interface Marker {
lat: number;
lng: number;
label?: string;
draggable: boolean;
}
interface Location {
lat: number;
lng: number;
viewport?: Object;
zoom: number;
address_level_1?: string;
address_level_2?: string;
address_country?: string;
address_zip?: string;
address_state?: string;
marker?: Marker;
}
@Component({
selector: 'search-map',
templateUrl: './search-map.component.html',
styleUrls: ['./search-map.component.scss'],
})
export class SearchMapComponent implements OnInit {
geocoder: any;
location: Location = {
lat: 51.678418,
lng: 7.809007,
marker: {
lat: 51.678418,
lng: 7.809007,
draggable: true
},
zoom: 5
};
@ViewChild(AgmMap, { static: true }) map!: AgmMap;
constructor(public mapsApiLoader: MapsAPILoader) {
this.mapsApiLoader = mapsApiLoader;
this.mapsApiLoader.load().then(() => {
this.geocoder = new google.maps.Geocoder();
});
}
updateOnMap() {
let full_address: string = this.location.address_level_1 || ""
if (this.location.address_level_2) { full_address = full_address + " " + this.location.address_level_2; }
if (this.location.address_state) { full_address = full_address + " " + this.location.address_state; }
if (this.location.address_country) { full_address = full_address + " " + this.location.address_country; }
this.findLocation(full_address);
}
findLocation(address) {
if (!this.geocoder) { this.geocoder = new google.maps.Geocoder(); }
this.geocoder.geocode({
'address': address
}, (results, status) => {
if (status == google.maps.GeocoderStatus.OK) {
for (var i = 0; i < results[0].address_components.length; i++) {
let types = results[0].address_components[i].types;
if (types.indexOf('locality') !== -1) {
this.location.address_level_2 = results[0].address_components[i].long_name;
}
if (types.indexOf('country') !== -1) {
this.location.address_country = results[0].address_components[i].long_name;
}
if (types.indexOf('postal_code') !== -1) {
this.location.address_zip = results[0].address_components[i].long_name;
}
if (types.indexOf('administrative_area_level_1') !== -1) {
this.location.address_state = results[0].address_components[i].long_name;
}
}
if (results[0].geometry.location) {
this.location.lat = results[0].geometry.location.lat();
this.location.lng = results[0].geometry.location.lng();
this.location.marker.lat = results[0].geometry.location.lat();
this.location.marker.lng = results[0].geometry.location.lng();
this.location.marker.draggable = true;
this.location.viewport = results[0].geometry.viewport;
}
this.map.triggerResize();
} else {
alert("Sorry, this search produced no results.");
}
});
}
findAddressByCoordinates() {
this.geocoder.geocode({
'location': {
lat: this.location.marker.lat,
lng: this.location.marker.lng
}
}, (results, status) => {
this.decomposeAddressComponents(results);
});
}
decomposeAddressComponents(addressArray) {
if (addressArray.length == 0) { return false; }
let address = addressArray[0].address_components;
for (let element of address) {
if (element.length == 0 && !element['types']) { continue; }
if (element['types'].indexOf('street_number') > -1) {
this.location.address_level_1 = element['long_name'];
continue;
}
if (element['types'].indexOf('route') > -1) {
this.location.address_level_1 += ', ' + element['long_name'];
continue;
}
if (element['types'].indexOf('locality') > -1) {
this.location.address_level_2 = element['long_name'];
continue;
}
if (element['types'].indexOf('administrative_area_level_1') > -1) {
this.location.address_state = element['long_name'];
continue;
}
if (element['types'].indexOf('country') > -1) {
this.location.address_country = element['long_name'];
continue;
}
if (element['types'].indexOf('postal_code') > -1) {
this.location.address_zip = element['long_name'];
continue;
}
}
}
}
Angular Google Maps - API
In this section you will find informations about required modules and available inputs of forms component.
Modules used
In order to speed up your application, you can choose to import only the modules you actually need, instead of importing the entire MDB Angular library. Remember that importing the entire library, and immediately afterwards a specific module, is bad practice, and can cause application errors.
import { ButtonsModule, InputsModule } from 'ng-uikit-pro-standard'
import { ButtonsModule, InputsModule } from 'angular-bootstrap-md'
import { AgmCoreModule } from '@agm/core'
Add AgmCoreModule.forRoot({ apiKey: 'your key here' }) in the imports declarations, in the app.module.ts file to see the map (get an API key here).
npm i --save @agm/core
import { AgmCoreModule } from '@agm/core'
@NgModule({
imports: [
AgmCoreModule.forRoot({
apiKey: 'your key here'
})
],
})
Inputs
Name | Type | Default | Description | Example |
---|---|---|---|---|
clickableIcons |
boolean | true | When false, map icons are not clickable. A map icon represents a point of interest, also known as a POI. By default map icons are clickable. | [clickableIcons]="false" |
disableDefaultUI |
boolean | false | Enables/disables all default UI of the Google map. Please note: When the map is created, this value cannot get updated. | [disableDefaultUI]="true" |
disableDoubleClickZoom |
boolean | false | Enables/disables zoom and center on double click. Enabled by default. | [disableDoubleClickZoom]="true" |
draggableCursor |
string | The name or url of the cursor to display when mousing over a draggable map. This property uses the css * cursor attribute to change the icon. As with the css property, you must specify at least one fallback cursor that is not a URL. For example: [draggableCursor]="'url(http://www.example.com/icon.webp), auto;'" | [draggableCursor]="'url(http://www.example.com/icon.webp), auto;'" |
|
draggingCursor |
string | The name or url of the cursor to display when the map is being dragged. This property uses the css cursor attribute to change the icon. As with the css property, you must specify at least one fallback cursor that is not a URL. For example: [draggingCursor]="'url(http://www.example.com/icon.webp), auto;'" | ||
fitBounds |
LatLngBoundsLiteral | LatLngBounds | Sets the viewport to contain the given bounds. | [ fitBounds]="{}" |
|
fullscreenControl |
boolean | false | The initial enabled/disabled state of the Fullscreen control. | [fullscreenControl]="true" |
fullscreenControlOptions |
FullscreenControlOptions | Options for the Fullscreen control. | [ fullscreenControlOptions]="{}" |
|
gestureHandling |
"cooperative" | "greedy" | "none" | "auto" | 'auto' | This setting controls how gestures on the map are handled. Allowed values:
|
[ gestureHandling]="'none'" |
keyboardShortcuts |
boolean | true | If false, prevents the map from being controlled by the keyboard. Keyboard shortcuts are enabled by default. | [keyboardShortcuts]="false" |
latitude |
number | 0 | The latitude that defines the center of the map. | [latitude]="0" |
longitude |
number | 0 | The longitude that defines the center of the map. | [longitude]="0" |
mapDraggable |
boolean | true | Enables/disables if map is draggable. | [mapDraggable]="true" |
mapTypeControl |
boolean | false | The initial enabled/disabled state of the Map type control. | [mapTypeControl]="true" |
mapTypeControlOptions |
MapTypeControlOptions | Options for the Map type control. | [mapTypeControlOptions]="{}" |
|
mapTypeId |
"roadmap" | "hybrid" | "satellite" | "terrain" | string | 'roadmap' | The map mapTypeId. Defaults to 'roadmap'. | [mapTypeId]="'roadmap'" |
maxZoom |
number | The maximal zoom level of the map allowed. When not provided, no restrictions to the zoom level are enforced. | [maxZoom]="10" |
|
minZoom |
number | The minimum zoom level of the map allowed. When not provided, no restrictions to the zoom level are enforced. | [minZoom]="10" |
|
panControl |
boolean | false | The initial enabled/disabled state of the Pan control. | [panControl]="10" |
panControlOptions |
PanControlOptions | Options for the Pan control. | [panControlOptions]="{}" |
|
rotateControl |
boolean | false | The initial enabled/disabled state of the Rotate control. | [rotateControl]="false" |
rotateControlOptions |
RotateControlOptions | Options for the Rotate control. | [rotateControlOptions]="{}" |
|
scaleControl |
boolean | false | The initial enabled/disabled state of the Scale control. This is disabled by default. | [scaleControl]="true" |
scaleControlOptions |
ScaleControlOptions | Options for the scale control. | [scaleControlOptions]="{}" |
|
scrollwheel |
boolean | true | If false, disables scrollwheel zooming on the map. The scrollwheel is enabled by default. | [scrollwheel]="{}" |
streetViewControl |
boolean | true | The initial enabled/disabled state of the Street View Pegman control. This control is part of the default UI, and should be set to false when displaying a map type on which the Street View road overlay should not appear (e.g. a non-Earth map type). | [streetViewControl]="true" |
streetViewControlOptions |
StreetViewControlOptions | {} | Options for the Street View control. | [streetViewControlOptions]="{}" |
styles |
MapTypeStyle[] | [] | Styles to apply to each of the default map types. Note that for Satellite/Hybrid and Terrain modes, these styles will only apply to labels and geometry. | [styles]="[]" |
usePanning |
boolean | false | When true and the latitude and/or longitude values changes, the Google Maps panTo method is used to center the map. See: https://developers.google.com/maps/documentation/javascript/reference#Map | [usePanning]="true" |
zoom |
number | 8 | The zoom level of the map. The default zoom level is 8. | [zoom]="10" |
zoomControl |
boolean | true | The enabled/disabled state of the Zoom control. | [zoomControl]="false" |
zoomControlOptions |
ZoomControlOptions | Options for the Zoom control. | [zoomControlOptions]="{}" |