149 lines
18 KiB
JavaScript
149 lines
18 KiB
JavaScript
|
/**
|
||
|
* @license
|
||
|
* Copyright Google LLC All Rights Reserved.
|
||
|
*
|
||
|
* Use of this source code is governed by an MIT-style license that can be
|
||
|
* found in the LICENSE file at https://angular.io/license
|
||
|
*/
|
||
|
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
|
||
|
/// <reference types="google.maps" preserve="true" />
|
||
|
import { Directive, EventEmitter, Input, NgZone, Output, inject, } from '@angular/core';
|
||
|
import { Observable } from 'rxjs';
|
||
|
import { GoogleMap } from '../google-map/google-map';
|
||
|
import { MapEventManager } from '../map-event-manager';
|
||
|
import * as i0 from "@angular/core";
|
||
|
import * as i1 from "../google-map/google-map";
|
||
|
/**
|
||
|
* Angular component that renders a Google Maps Directions Renderer via the Google Maps
|
||
|
* JavaScript API.
|
||
|
*
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions#DirectionsRenderer
|
||
|
*/
|
||
|
export class MapDirectionsRenderer {
|
||
|
/**
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions
|
||
|
* #DirectionsRendererOptions.directions
|
||
|
*/
|
||
|
set directions(directions) {
|
||
|
this._directions = directions;
|
||
|
}
|
||
|
/**
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions
|
||
|
* #DirectionsRendererOptions
|
||
|
*/
|
||
|
set options(options) {
|
||
|
this._options = options;
|
||
|
}
|
||
|
constructor(_googleMap, _ngZone) {
|
||
|
this._googleMap = _googleMap;
|
||
|
this._ngZone = _ngZone;
|
||
|
this._eventManager = new MapEventManager(inject(NgZone));
|
||
|
/**
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions
|
||
|
* #DirectionsRenderer.directions_changed
|
||
|
*/
|
||
|
this.directionsChanged = this._eventManager.getLazyEmitter('directions_changed');
|
||
|
/** Event emitted when the directions renderer is initialized. */
|
||
|
this.directionsRendererInitialized = new EventEmitter();
|
||
|
}
|
||
|
ngOnInit() {
|
||
|
if (this._googleMap._isBrowser) {
|
||
|
if (google.maps.DirectionsRenderer && this._googleMap.googleMap) {
|
||
|
this._initialize(this._googleMap.googleMap, google.maps.DirectionsRenderer);
|
||
|
}
|
||
|
else {
|
||
|
this._ngZone.runOutsideAngular(() => {
|
||
|
Promise.all([this._googleMap._resolveMap(), google.maps.importLibrary('routes')]).then(([map, lib]) => {
|
||
|
this._initialize(map, lib.DirectionsRenderer);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
_initialize(map, rendererConstructor) {
|
||
|
// Create the object outside the zone so its events don't trigger change detection.
|
||
|
// We'll bring it back in inside the `MapEventManager` only for the events that the
|
||
|
// user has subscribed to.
|
||
|
this._ngZone.runOutsideAngular(() => {
|
||
|
this.directionsRenderer = new rendererConstructor(this._combineOptions());
|
||
|
this._assertInitialized();
|
||
|
this.directionsRenderer.setMap(map);
|
||
|
this._eventManager.setTarget(this.directionsRenderer);
|
||
|
this.directionsRendererInitialized.emit(this.directionsRenderer);
|
||
|
});
|
||
|
}
|
||
|
ngOnChanges(changes) {
|
||
|
if (this.directionsRenderer) {
|
||
|
if (changes['options']) {
|
||
|
this.directionsRenderer.setOptions(this._combineOptions());
|
||
|
}
|
||
|
if (changes['directions'] && this._directions !== undefined) {
|
||
|
this.directionsRenderer.setDirections(this._directions);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
ngOnDestroy() {
|
||
|
this._eventManager.destroy();
|
||
|
this.directionsRenderer?.setMap(null);
|
||
|
}
|
||
|
/**
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions
|
||
|
* #DirectionsRenderer.getDirections
|
||
|
*/
|
||
|
getDirections() {
|
||
|
this._assertInitialized();
|
||
|
return this.directionsRenderer.getDirections();
|
||
|
}
|
||
|
/**
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions
|
||
|
* #DirectionsRenderer.getPanel
|
||
|
*/
|
||
|
getPanel() {
|
||
|
this._assertInitialized();
|
||
|
return this.directionsRenderer.getPanel();
|
||
|
}
|
||
|
/**
|
||
|
* See developers.google.com/maps/documentation/javascript/reference/directions
|
||
|
* #DirectionsRenderer.getRouteIndex
|
||
|
*/
|
||
|
getRouteIndex() {
|
||
|
this._assertInitialized();
|
||
|
return this.directionsRenderer.getRouteIndex();
|
||
|
}
|
||
|
_combineOptions() {
|
||
|
const options = this._options || {};
|
||
|
return {
|
||
|
...options,
|
||
|
directions: this._directions || options.directions,
|
||
|
map: this._googleMap.googleMap,
|
||
|
};
|
||
|
}
|
||
|
_assertInitialized() {
|
||
|
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
||
|
if (!this.directionsRenderer) {
|
||
|
throw Error('Cannot interact with a Google Map Directions Renderer before it has been ' +
|
||
|
'initialized. Please wait for the Directions Renderer to load before trying ' +
|
||
|
'to interact with it.');
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.0-next.2", ngImport: i0, type: MapDirectionsRenderer, deps: [{ token: i1.GoogleMap }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive }); }
|
||
|
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.0-next.2", type: MapDirectionsRenderer, isStandalone: true, selector: "map-directions-renderer", inputs: { directions: "directions", options: "options" }, outputs: { directionsChanged: "directionsChanged", directionsRendererInitialized: "directionsRendererInitialized" }, exportAs: ["mapDirectionsRenderer"], usesOnChanges: true, ngImport: i0 }); }
|
||
|
}
|
||
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0-next.2", ngImport: i0, type: MapDirectionsRenderer, decorators: [{
|
||
|
type: Directive,
|
||
|
args: [{
|
||
|
selector: 'map-directions-renderer',
|
||
|
exportAs: 'mapDirectionsRenderer',
|
||
|
standalone: true,
|
||
|
}]
|
||
|
}], ctorParameters: () => [{ type: i1.GoogleMap }, { type: i0.NgZone }], propDecorators: { directions: [{
|
||
|
type: Input
|
||
|
}], options: [{
|
||
|
type: Input
|
||
|
}], directionsChanged: [{
|
||
|
type: Output
|
||
|
}], directionsRendererInitialized: [{
|
||
|
type: Output
|
||
|
}] } });
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFwLWRpcmVjdGlvbnMtcmVuZGVyZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvZ29vZ2xlLW1hcHMvbWFwLWRpcmVjdGlvbnMtcmVuZGVyZXIvbWFwLWRpcmVjdGlvbnMtcmVuZGVyZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBRUgseUVBQXlFO0FBQ3pFLHFEQUFxRDtBQUVyRCxPQUFPLEVBQ0wsU0FBUyxFQUNULFlBQVksRUFDWixLQUFLLEVBQ0wsTUFBTSxFQUlOLE1BQU0sRUFFTixNQUFNLEdBQ1AsTUFBTSxlQUFlLENBQUM7QUFDdkIsT0FBTyxFQUFDLFVBQVUsRUFBQyxNQUFNLE1BQU0sQ0FBQztBQUNoQyxPQUFPLEVBQUMsU0FBUyxFQUFDLE1BQU0sMEJBQTBCLENBQUM7QUFDbkQsT0FBTyxFQUFDLGVBQWUsRUFBQyxNQUFNLHNCQUFzQixDQUFDOzs7QUFFckQ7Ozs7O0dBS0c7QUFNSCxNQUFNLE9BQU8scUJBQXFCO0lBR2hDOzs7T0FHRztJQUNILElBQ0ksVUFBVSxDQUFDLFVBQXdDO1FBQ3JELElBQUksQ0FBQyxXQUFXLEdBQUcsVUFBVSxDQUFDO0lBQ2hDLENBQUM7SUFHRDs7O09BR0c7SUFDSCxJQUNJLE9BQU8sQ0FBQyxPQUE4QztRQUN4RCxJQUFJLENBQUMsUUFBUSxHQUFHLE9BQU8sQ0FBQztJQUMxQixDQUFDO0lBa0JELFlBQ21CLFVBQXFCLEVBQzlCLE9BQWU7UU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