src/directives/map-directions.ts
Renders directions on heremap. Please note that directive must be placed inside map component, otherwise it will never be rendered.
Providers |
{
provide: BaseMapComponent, useExisting: forwardRef(() => MapDirectionsDirective)
}
|
Selector | map-directions |
Properties |
|
Methods |
|
Inputs |
Outputs |
Accessors |
constructor(_mapsManager: HereMapsManager)
|
||||||
Defined in src/directives/map-directions.ts:163
|
||||||
Parameters :
|
destination
|
Destination of directions |
Defined in src/directives/map-directions.ts:74
|
intermediatePoints
|
Destination of directions
Type : |
Defined in src/directives/map-directions.ts:89
|
origin
|
Origin of directions |
Defined in src/directives/map-directions.ts:59
|
preserveViewport
|
By default, the input map is centered and zoomed to the bounding box of this set of directions. If this option is set to true, the viewport is left unchanged, unless the map's center and zoom were never set.
Default value : |
Defined in src/directives/map-directions.ts:154
|
route
|
|
Defined in src/directives/map-directions.ts:44
|
directions_changed
|
This event is fired when the directions route changes. $event Type: EventEmitter<void>
|
Defined in src/directives/map-directions.ts:148
|
Private bindEvents |
bindEvents()
|
Defined in src/directives/map-directions.ts:260
|
Returns :
void
|
Public hasMapComponent |
hasMapComponent()
|
Defined in src/directives/map-directions.ts:182
|
Returns :
boolean
|
Public ngOnDestroy |
ngOnDestroy()
|
Defined in src/directives/map-directions.ts:205
|
Returns :
void
|
Private renderRoute | ||||||
renderRoute(route: GeoPoint[])
|
||||||
Defined in src/directives/map-directions.ts:244
|
||||||
Parameters :
Returns :
void
|
Public setMapComponent | ||||||||||||
setMapComponent(component: MapComponent, map: H.Map, ui: H.ui.UI)
|
||||||||||||
Defined in src/directives/map-directions.ts:186
|
||||||||||||
Parameters :
Returns :
void
|
Private tryShowRoute |
tryShowRoute()
|
Defined in src/directives/map-directions.ts:213
|
Returns :
void
|
Private _destination |
_destination:
|
Type : GeoPoint
|
Defined in src/directives/map-directions.ts:157
|
Private _fillColor |
_fillColor:
|
Type : string
|
Defined in src/directives/map-directions.ts:161
|
Private _intermediatePoints |
_intermediatePoints:
|
Type : GeoPoint[]
|
Default value : []
|
Defined in src/directives/map-directions.ts:158
|
Private _lineWidth |
_lineWidth:
|
Type : number
|
Defined in src/directives/map-directions.ts:159
|
Private _origin |
_origin:
|
Type : GeoPoint
|
Defined in src/directives/map-directions.ts:156
|
Private _route |
_route:
|
Type : Array<LatLng> | DirectionsResolver
|
Defined in src/directives/map-directions.ts:163
|
Private _strokeColor |
_strokeColor:
|
Type : string
|
Defined in src/directives/map-directions.ts:160
|
Protected mapComponent |
mapComponent:
|
Type : MapComponent
|
Defined in src/directives/map-directions.ts:41
|
route | ||||
getroute()
|
||||
Defined in src/directives/map-directions.ts:50
|
||||
setroute(value)
|
||||
Defined in src/directives/map-directions.ts:44
|
||||
Parameters :
Returns :
void
|
origin | ||||||
getorigin()
|
||||||
Defined in src/directives/map-directions.ts:65
|
||||||
setorigin(value)
|
||||||
Defined in src/directives/map-directions.ts:59
|
||||||
Origin of directions
Parameters :
Returns :
void
|
destination | ||||||
getdestination()
|
||||||
Defined in src/directives/map-directions.ts:80
|
||||||
setdestination(value)
|
||||||
Defined in src/directives/map-directions.ts:74
|
||||||
Destination of directions
Parameters :
Returns :
void
|
intermediatePoints | ||||||||
getintermediatePoints()
|
||||||||
Defined in src/directives/map-directions.ts:95
|
||||||||
setintermediatePoints(value: [])
|
||||||||
Defined in src/directives/map-directions.ts:89
|
||||||||
Destination of directions
Parameters :
Returns :
void
|
lineWidth | ||||||
getlineWidth()
|
||||||
Defined in src/directives/map-directions.ts:110
|
||||||
setlineWidth(value: number)
|
||||||
Defined in src/directives/map-directions.ts:99
|
||||||
Parameters :
Returns :
void
|
strokeColor | ||||||
getstrokeColor()
|
||||||
Defined in src/directives/map-directions.ts:125
|
||||||
setstrokeColor(value: string)
|
||||||
Defined in src/directives/map-directions.ts:114
|
||||||
Parameters :
Returns :
void
|
fillColor | ||||||
getfillColor()
|
||||||
Defined in src/directives/map-directions.ts:140
|
||||||
setfillColor(value: string)
|
||||||
Defined in src/directives/map-directions.ts:129
|
||||||
Parameters :
Returns :
void
|
import {
Directive,
Input,
Output,
OnDestroy,
EventEmitter,
forwardRef
} from '@angular/core';
import { HereMapsManager } from '../services/maps-manager';
import { BaseMapComponent } from './base-map-component';
import { GeoPoint, LatLng } from '../interface/lat-lng';
import { toLatLng } from '../utils/position';
import { MapComponent } from './map';
export type DirectionsResolver = (
origin: LatLng,
destination: LatLng
) => Promise<LatLng[]>;
/**
* Renders directions on heremap. Please note that directive must be placed inside
* map component, otherwise it will never be rendered.
*/
@Directive({
selector: 'map-directions',
providers: [
{
provide: BaseMapComponent,
useExisting: forwardRef(() => MapDirectionsDirective)
}
]
})
export class MapDirectionsDirective extends BaseMapComponent<H.map.Polyline>
implements OnDestroy {
// private _originMarker: H.map.Marker;
// private _destinationMarker: H.map.Marker;
protected mapComponent: MapComponent;
@Input()
set route(value: Array<LatLng> | DirectionsResolver) {
if (this._route !== value) {
this._route = value;
this.tryShowRoute();
}
}
get route(): Array<LatLng> | DirectionsResolver {
return this._route;
}
/**
* Origin of directions
* @param value can be google.maps.LatLngLiteral or Coordinates or {latitude: number, longitude: number}
*/
@Input()
set origin(value: GeoPoint) {
if (this._origin !== value) {
this._origin = value;
this.tryShowRoute();
}
}
get origin(): GeoPoint {
return this._origin;
}
/**
* Destination of directions
* @param value can be google.maps.LatLngLiteral or Coordinates or {latitude: number, longitude: number}
*/
@Input()
set destination(value: GeoPoint) {
if (this._destination !== value) {
this._destination = value;
this.tryShowRoute();
}
}
get destination(): GeoPoint {
return this._destination;
}
/**
* Destination of directions
* @param value can be google.maps.LatLngLiteral or Coordinates or {latitude: number, longitude: number}
*/
@Input()
set intermediatePoints(value: GeoPoint[]) {
if (this._intermediatePoints !== value) {
this._intermediatePoints = value;
this.tryShowRoute();
}
}
get intermediatePoints(): GeoPoint[] {
return this._intermediatePoints;
}
set lineWidth(value: number) {
if (this._lineWidth !== value) {
this._lineWidth = value;
this.proxy.then(route => {
let style = route.getStyle();
style = new H.map.SpatialStyle();
style.lineWidth = this._lineWidth;
route.setStyle(style);
});
}
}
get lineWidth(): number {
return this._lineWidth;
}
set strokeColor(value: string) {
if (this._strokeColor !== value) {
this._strokeColor = value;
this.proxy.then(route => {
let style = route.getStyle();
style = new H.map.SpatialStyle();
style.strokeColor = value;
route.setStyle(style);
});
}
}
get strokeColor(): string {
return this._strokeColor;
}
set fillColor(value: string) {
if (this._fillColor !== value) {
this._fillColor = value;
this.proxy.then(route => {
let style = route.getStyle();
style = new H.map.SpatialStyle();
style.fillColor = value;
route.setStyle(style);
});
}
}
get fillColor(): string {
return this._fillColor;
}
/**
* This event is fired when the directions route changes.
*/
@Output()
directions_changed: EventEmitter<void> = new EventEmitter<void>();
/**
* By default, the input map is centered and zoomed to the bounding box of this set of directions.
* If this option is set to true, the viewport is left unchanged, unless the map's center and zoom were never set.
*/
@Input()
public preserveViewport = true;
private _origin: GeoPoint;
private _destination: GeoPoint;
private _intermediatePoints: GeoPoint[] = [];
private _lineWidth: number;
private _strokeColor: string;
private _fillColor: string;
private _route: Array<LatLng> | DirectionsResolver;
constructor(private _mapsManager: HereMapsManager) {
super();
this._mapsManager.onApiLoad().then(() => {
const lineString = new H.geo.LineString([
44.09,
-116.9,
3000,
44.082305,
-116.776059,
2000
]);
const route = new H.map.Polyline(lineString);
this.proxyResolver(route);
});
}
public hasMapComponent(): boolean {
return !!this.mapComponent;
}
public setMapComponent(
component: MapComponent,
map: H.Map,
ui: H.ui.UI
): void {
this.mapComponent = component;
this.proxy.then((mapObject: H.map.Polyline) =>
setTimeout(() => {
if (mapObject instanceof H.map.Object) {
map.addObject(mapObject);
}
}, this.delay || 0)
);
}
/*
* Internal logic
* **********************************************************
*/
public ngOnDestroy(): void {
this.mapComponent.getMap().then(map => {
this.proxy.then(polyline => {
polyline.dispose();
});
});
}
private tryShowRoute() {
const route = this._route || [];
if (route instanceof Array && route.length > 0) {
this.renderRoute(route);
} else if (this.origin && this.destination) {
this.renderRoute([]);
this._mapsManager
.getDirections(
toLatLng(this.origin),
toLatLng(this.destination),
this.intermediatePoints || []
)
.then(r => {
if (r && r.response && r.response.route) {
const newRoute = r.response.route[0].shape.map(str => {
const parts = str.split(',');
return { lat: parseFloat(parts[0]), lng: parseFloat(parts[1]) };
});
this.renderRoute(newRoute || []);
} else {
this.renderRoute([]);
}
})
.catch(e => {
this.renderRoute([]);
});
} else {
this.renderRoute([]);
}
}
private renderRoute(route: GeoPoint[]) {
this.proxy.then(polyline => {
if (route instanceof Array && route.length > 0) {
const lineString = new H.geo.LineString([]);
route.forEach(point => {
lineString.pushPoint(toLatLng(point));
});
polyline.setGeometry(lineString);
polyline.setVisibility(route.length > 0);
} else {
polyline.setVisibility(false);
return;
}
});
}
private bindEvents() {
// this.proxy.then()
// directions.addListener('directions_changed', (e) => this.directions_changed.emit(e));
}
}