@mapify/angular
v0.7.1
Published
Mapify angular components library
Downloads
121
Keywords
Readme
README
Summary
- Mapify Angular - an angular library with components for the Mapify platform
The @mapify/angular
project contains the Mapify angular components which can be used to ease the visualisation of Mapify maps and data.
Further Documentation and Blog Posts
You can checkout the following blog post for an example on setting up an Angular App.
For further information and blog posts you can go here.
General Setup
You will need to have nodejs
and npm
installed.
You will need a Mapify account in order to get an API Key to use when configuring the SDK and other components.
- Get your desired API Key from the Mapify console.
Mapify Angular Lib
Angular Lib Setup
Install @mapify/angular
from npm:
npm i @mapify/angular @angular/cdk@13 @angular/material@13 --save
then add to polyfills.ts:
import 'reflect-metadata';
The Angular library requires:
- A Mapify API Key in order to access the Mapify platform. Get your API Key from the Mapify console.
- A Google Maps API Key in order to use the Google Maps services.
Angular Lib Configuration
In order to use the Angular components provided by this library, you will need to configure the module when importing it in your app's module.
In your app's module import MapifyAngularModule with the configuration required:
import { MapifyAngularModule } from '@mapify/angular'
import { MapConfig } from '@mapify/core';
@NgModule({
imports: [
MapifyAngularModule.forRoot(new MapConfig(
// Your API keys
environment.mapifyApiKey,
environment.googleMapsApiKey
))
]
...
})
export class YourAppModule { }
From this point onward, you will be able to use components provided in the library.
Using the Map Component from the Mapify Angular Lib
In you component's html add the mapify-map
component:
<mapify-map [map]="map" #mapifymap> </mapify-map>
In your component initialize and use the MapifyClient in order to get your maps:
import { MapifyClient, Map } from '@mapify/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
@ViewChild('mapifymap') mapifyMap: MapifyMapComponent;
public map: Map;
constructor() {
const clientConfig: ClientConfig = {
// Your API key
apiKey: environment.mapifyApiKey,
};
this.mapifyClient = MapifyClient.createClient(clientConfig);
}
public async ngOnInit(): Promise<void> {
// Get some maps from Mapify
const maps = await this.mapifyClient.maps.getAllPaginated();
if (maps?.items?.length <= 0) {
return;
}
const desiredMap = maps.items[0];
// Request the map specifically to get the full data needed to display the map
this.map = await this.mapifyClient.maps.get(desiredMap.mapId);
}
}
By updating the contents of this.map
the Mapify map component will detect changes and react to them. By using the ViewChild mapifyMap
we can gain access to further methods that help us manipulate the map we have provided.
For example in order to show the map use:
this.mapifyMap.showMap();
Or to hide the map:
this.mapifyMap.hideMap();
Or to query the map's visibility:
this.mapifyMap.isMapVisible();
In order to center the map use:
this.mapifyMap.centerMap();
NOTE: In order to be able to manipulate the map, you will need to wait for the map to be set in the component. For example, you will not be able to toggle the map in the same
ngOnInit
method in which you changed the content ofthis.map
. This is because at that point no changes have been detected yet, thus only in the following cycles will the component render the map data provided.
In order to start the map with layers visible/centered there are two approaches you can choose from:
onMapReady
The first approach is to send a handler to the mapReady event emitter, that toggles the map visibility and centers the map once the map is ready.
Example usage:
export class AppComponent implements OnInit {
...
// Define the method in your component
public onMapReady() {
this.mapifyMap.showMap();
this.mapifyMap.centerMap();
}
}
<mapify-map [map]="map" (mapReady)="onMapReady()" #mapifymap> </mapify-map>
Further Map Configuration
The mapify-map
component can be further configured. Optional inputs include:
- googleMapsMapOptions
- height
- width
The googleMapsMapOptions
gives control of the base map and its properties. For more information on this interface check Google's documentation here.
The height
property allows the height of the map container to be defined. By default it is set to '100%'
. This string should reflect a CSS string's format to apply to the map's container's height style.
The width
property allows the width of the map container to be defined. By default it is set to '100%'
. This string should reflect a CSS string's format to apply to the map's container's width style.
Example
In you component's html add the mapify-map
component:
<mapify-map
#mapifymap
[map]="map"
[googleMapsMapOptions]="googleMapsMapOptions"
height="height"
width="width"
(mapReady)="onMapReady()"
>
</mapify-map>
In your component initialize and use the configurations:
...
export class AppComponent implements OnInit {
@ViewChild('mapifymap') mapifyMap: MapifyMapComponent;
public map: Map;
public height: '500px';
public width: '500px';
public googleMapsMapOptions: google.maps.MapOptions = {
backgroundColor: '#2B2B2B',
zoomControl: false,
scrollwheel: true,
disableDoubleClickZoom: true,
fullscreenControl: false,
mapTypeControl: false,
streetViewControl: false,
maxZoom: 20,
minZoom: 1,
zoom: 3,
}
constructor() {}
...
}