File

src/directives/map.ts

Description

Component that should render and initialize map instance. Use it to define point in html document where map should be rendered

Implements

OnDestroy OnInit AfterContentInit

Metadata

providers { provide: MapUIService, useClass: MapUIService }
selector heremap
styles :host {width: 100%; height: 100%}
template
<div class="heremap-container" style="width: inherit; height: inherit"></div>
<ng-content></ng-content>

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor(_name: string, _elem: ElementRef, _mapsManager: HereMapsManager)
Parameters :
Name Type Optional
_name string No
_elem ElementRef No
_mapsManager HereMapsManager No

Inputs

animateZoom

Default value : true

autoFitMarkers

Should map auto resize bounds to current set of markers

Default value : true

backgroundColor

Color used for the background of the Map div. This color will be visible when tiles have not yet loaded as the user pans. Note: This option can only be set when the map is initialized.

Type : string

center

The initial Map center. Required.

disableDefaultUI

Enables/disables all default UI.

Default value : false

draggable

If false, prevents the map from being dragged. Dragging is enabled by default.

Default value : true

enableDoubleClickZoom

Enables/disables zoom and center on double click. Enabled by default.

Default value : true

keyboardShortcuts

If false, prevents the map from being controlled by the keyboard. Keyboard shortcuts are enabled by default.

Default value : true

mapTypeControl

Enabled/Disabled state of the Map type control.

Default value : false

maxZoom

The maximum zoom level which will be displayed on the map.

Type : number

minZoom

The minimum zoom level which will be displayed on the map.

Type : number

rotateControl

Enabled/Disabled state of the Rotate control.

Default value : false

scaleControl

Enabled/Disabled state of the Scale control.

Default value : true

scrollwheel

If false, disables scrollwheel zooming on the map. The scrollwheel is enabled by default.

Default value : true

streetViewControl

Enabled/Disabled state of the Street View Pegman control.

Default value : false

zoom

Map zoom level.

Default value : 5

zoomControl

Enabled/Disabled state of the Zoom control

Default value : true

Outputs

clickMap

Fires when user interacts with map by clicking on it

$event Type: EventEmitter
update

Notifies subscribers that map is updated

$event Type: EventEmitter

Methods

Private attachComponentsToMap
attachComponentsToMap()
Returns : void
Private attachEvents
attachEvents(map: H.Map)
Parameters :
Name Type Optional
map H.Map No
Returns : void
Public fitBounds
fitBounds(bounds: H.geo.Rect)

Fits map to given bounds

Parameters :
Name Type Optional
bounds H.geo.Rect No
Returns : void
Private getControlOptions
getControlOptions()
Returns : IControlOptions
getMap
getMap()

When resolved, returns Heremap instance

Returns : Promise<H.Map>
Private getOptions
getOptions()
Returns : H.Map.Options
Private latLngCenter
latLngCenter()
Returns : LatLng
ngAfterContentInit
ngAfterContentInit()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Private resetMapBounds
resetMapBounds(bounds: H.geo.Rect)
Parameters :
Name Type Optional
bounds H.geo.Rect No
Returns : void
toString
toString()
Returns : string

Properties

Private _center
_center: LatLng | literal type
Type : LatLng | literal type
Private _id
_id: number
Type : number
Private _map
_map: Promise<H.Map>
Type : Promise<H.Map>
Private _mapBackgroundColor
_mapBackgroundColor: string
Type : string
Private _mapComponentsSubscriptions
_mapComponentsSubscriptions: Subscription
Type : Subscription
Private _mapResolver
_mapResolver: function
Type : function
Protected _uiResolver
_uiResolver: function
Type : function
Static counters
counters: number
Type : number
Default value : 0
Public mapComponents
mapComponents: QueryList<BaseMapComponent<H.map.Object>>
Type : QueryList<BaseMapComponent<H.map.Object>>
Decorators :
@ContentChildren(undefined, {})
Public Readonly ui
ui:
Default value : new Promise<H.ui.UI>( resolve => (this._uiResolver = resolve) )

Accessors

backgroundColor
setbackgroundColor(value: string)

Color used for the background of the Map div. This color will be visible when tiles have not yet loaded as the user pans. Note: This option can only be set when the map is initialized.

Parameters :
Name Type Optional
value string No
Returns : void
center
getcenter()
setcenter(value)

The initial Map center. Required.

Parameters :
Name Optional
value No
Returns : void
import {
  Component,
  Input,
  OnInit,
  OnDestroy,
  AfterContentInit,
  forwardRef,
  ElementRef,
  QueryList,
  Attribute,
  ContentChildren,
  Output,
  EventEmitter
} from '@angular/core';

import { HereMapsManager } from '../services/maps-manager';
import { BaseMapComponent } from './base-map-component';
import { LatLng, GeoPoint } from '../interface/lat-lng';
import { MapUIService } from '../services/map-ui.service';
import { IControlOptions } from '../interface/control-options.interface';
import { toLatLng } from '../utils/position';
import { Subscription } from 'rxjs';

/**
 * Component that should render and initialize map instance.
 * Use it to define point in html document where map should be rendered
 */
@Component({
  selector: 'heremap',
  template: `
        <div class="heremap-container" style="width: inherit; height: inherit"></div>
        <ng-content></ng-content>
    `,
  styles: [':host {width: 100%; height: 100%}'],
  providers: [{ provide: MapUIService, useClass: MapUIService }]
})
export class MapComponent implements OnDestroy, OnInit, AfterContentInit {
  static counters = 0;

  @ContentChildren(forwardRef(() => BaseMapComponent), {})
  public mapComponents: QueryList<BaseMapComponent<H.map.Object>>;

  public readonly ui = new Promise<H.ui.UI>(
    resolve => (this._uiResolver = resolve)
  );

  /**
   * Should map auto resize bounds to current set of markers
   */
  @Input()
  public autoFitMarkers = true;

  /**
   * Color used for the background of the Map div.
   * This color will be visible when tiles have not yet loaded as the user pans.
   * Note: This option can only be set when the map is initialized.
   */
  @Input()
  set backgroundColor(value: string) {
    if (this._mapBackgroundColor) {
      console.warn(
        'Option "backgroundColor" can only be set when the map is initialized'
      );
      return;
    }

    this._mapBackgroundColor = value;
  }

  /**
   * The initial Map center. Required.
   */
  @Input()
  set center(value: GeoPoint) {
    this._map.then(map => {
      if (value) {
        map.setCenter(toLatLng(value));
      }
    });

    this._center = toLatLng(value);
  }
  get center() {
    return this._center;
  }

  /**
   * Enables/disables zoom and center on double click. Enabled by default.
   */
  @Input()
  public enableDoubleClickZoom = true;

  /**
   * If false, prevents the map from being dragged.
   * Dragging is enabled by default.
   */
  @Input()
  public draggable = true;

  /**
   * If false, prevents the map from being controlled by the keyboard.
   * Keyboard shortcuts are enabled by default.
   */
  @Input()
  public keyboardShortcuts = true;

  /**
   * If false, disables scrollwheel zooming on the map.
   * The scrollwheel is enabled by default.
   */
  @Input()
  public scrollwheel = true;

  /*
   * Zoom options
   * **********************************************************
   */

  /**
   * Map zoom level.
   */
  @Input()
  public zoom = 5;

  /**
   * The minimum zoom level which will be displayed on the map.
   */
  @Input()
  public minZoom: number;

  /**
   * The maximum zoom level which will be displayed on the map.
   */
  @Input()
  public maxZoom: number;

  /*
   * Control options
   * **********************************************************
   */

  /**
   * Enables/disables all default UI.
   */
  @Input()
  public disableDefaultUI = false;

  /**
   * Enabled/Disabled state of the Map type control.
   */
  @Input()
  public mapTypeControl = false;

  /**
   * Enabled/Disabled state of the Rotate control.
   */
  @Input()
  public rotateControl = false;

  /**
   * Enabled/Disabled state of the Scale control.
   */
  @Input()
  public scaleControl = true;

  /**
   * Enabled/Disabled state of the Street View Pegman control.
   */
  @Input()
  public streetViewControl = false;

  @Input()
  public animateZoom = true;

  /**
   * Enabled/Disabled state of the Zoom control
   */
  @Input()
  public zoomControl = true;

  /**
   * Notifies subscribers that map is updated
   */
  @Output()
  update = new EventEmitter<H.Map>();

  /**
   * Fires when user interacts with map by clicking on it
   */
  @Output()
  clickMap = new EventEmitter<H.mapevents.Event>();

  protected _uiResolver: (ui: H.ui.UI) => void;

  private _id: number;
  private _map: Promise<H.Map>;
  private _mapResolver: (map: H.Map) => void;
  private _mapBackgroundColor: string;

  private _mapComponentsSubscriptions: Subscription;

  private _center: LatLng | { latitude: number; longitude: number };

  constructor(
    @Attribute('name') private _name: string,
    private _elem: ElementRef,
    private _mapsManager: HereMapsManager
  ) {
    this._id = MapComponent.counters++;
    this._map = new Promise(resolve => (this._mapResolver = resolve));
  }

  /**
   * When resolved, returns Heremap instance
   */
  getMap(): Promise<H.Map> {
    return this._map;
  }

  /*
   * Internal logic
   * **********************************************************
   */

  ngOnInit(): void {
    this._mapsManager
      .createMap(
        this._elem.nativeElement.querySelector('.heremap-container'),
        this.getOptions(),
        this.getControlOptions()
      )
      .then(({ map: map, ui: ui, platform: platform }) => {
        this._mapsManager.addMap(this.toString(), map);
        this._uiResolver(ui);
        this._mapResolver(map);
        this.attachEvents(map);
      });
  }

  ngOnDestroy(): void {
    this._mapsManager.removeMap(this.toString());
    this._mapComponentsSubscriptions.unsubscribe();
  }

  ngAfterContentInit(): void {
    this._mapComponentsSubscriptions = this.mapComponents.changes.subscribe(
      () => {
        this.attachComponentsToMap();
      }
    );

    this.attachComponentsToMap();
  }

  toString(): string {
    return this._name ? this._name : `fh.here-maps-${this._id}`;
  }

  /**
   * Fits map to given bounds
   * @param bounds
   */
  public fitBounds(bounds: H.geo.Rect) {
    this.resetMapBounds(bounds);
  }

  private attachComponentsToMap(): void {
    this._map.then(map => {
      this.ui.then(ui => {
        this.mapComponents.filter(v => !v.hasMapComponent()).forEach(v => {
          v.setMapComponent(this, map, ui);
        });
      });
    });
  }

  private getOptions(): H.Map.Options {
    return {
      center: this.center ? this.latLngCenter() : { lat: 0, lng: 0 },
      zoom: this.zoom || 5
    };
  }

  private getControlOptions(): IControlOptions {
    return {
      mapTypeControl: this.mapTypeControl,
      rotateControl: this.rotateControl,
      scaleControl: this.scaleControl,
      streetViewControl: this.streetViewControl,
      zoomControl: this.zoomControl,
      enableDoubleClickZoom: this.enableDoubleClickZoom,
      draggable: this.draggable,
      keyboardShortcuts: this.keyboardShortcuts,
      scrollwheel: this.scrollwheel
    };
  }

  private resetMapBounds(bounds: H.geo.Rect) {
    this._map.then(map => {
      map.setViewBounds(bounds, this.animateZoom);
    });
  }

  private attachEvents(map: H.Map) {
    map.addEventListener('mapviewchangeend', () => {
      this.update.emit(map);
    });

    map.addEventListener('tap', (e: any) => {
      const pointer = e.currentPointer;
      const coordinates = map.screenToGeo(pointer.viewportX, pointer.viewportY);

      this.clickMap.emit({ ...e, coordinates });
    });
  }

  private latLngCenter(): LatLng {
    const lat = (this.center as any).lat
      ? (this.center as any).lat
      : (this.center as any).latitude;
    const lng = (this.center as any).lng
      ? (this.center as any).lng
      : (this.center as any).longitude;

    return { lat, lng };
  }
}
:host {width: 100%; height: 100%}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""