import {Directive, EventEmitter, OnChanges, OnDestroy, SimpleChange, AfterContentInit, ContentChildren, QueryList, Input, Output } from '@angular/core'; import {Subscription} from 'rxjs/Subscription'; import {MouseEvent} from '../map-types'; import * as mapTypes from '../services/google-maps-types'; import {MarkerManager} from '../services/managers/marker-manager'; import {AgmInfoWindow} from './info-window'; let markerId = 0; /** * AgmMarker renders a map marker inside a {@link AgmMap}. * * ### Example * ```typescript * import { Component } from '@angular/core'; * * @Component({ * selector: 'my-map-cmp', * styles: [` * .agm-map-container { * height: 300px; * } * `], * template: ` * * * * * ` * }) * ``` */ @Directive({ selector: 'agm-marker' }) export class AgmMarker implements OnDestroy, OnChanges, AfterContentInit { /** * The latitude position of the marker. */ @Input() latitude: number; /** * The longitude position of the marker. */ @Input() longitude: number; /** * The title of the marker. */ @Input() title: string; /** * The label (a single uppercase character) for the marker. */ @Input() label: string; /** * If true, the marker can be dragged. Default value is false. */ // tslint:disable-next-line:no-input-rename @Input('markerDraggable') draggable: boolean = false; /** * Icon (the URL of the image) for the foreground. */ @Input() iconUrl: string; /** * If true, the marker is visible */ @Input() visible: boolean = true; /** * Whether to automatically open the child info window when the marker is clicked. */ @Input() openInfoWindow: boolean = true; /** * The marker's opacity between 0.0 and 1.0. */ @Input() opacity: number = 1; /** * All markers are displayed on the map in order of their zIndex, with higher values displaying in * front of markers with lower values. By default, markers are displayed according to their * vertical position on screen, with lower markers appearing in front of markers further up the * screen. */ @Input() zIndex: number = 1; /** * This event emitter gets emitted when the user clicks on the marker. */ @Output() markerClick: EventEmitter = new EventEmitter(); /** * This event is fired when the user stops dragging the marker. */ @Output() dragEnd: EventEmitter = new EventEmitter(); /** * This event is fired when the user mouses over the marker. */ @Output() mouseOver: EventEmitter = new EventEmitter(); /** * This event is fired when the user mouses outside the marker. */ @Output() mouseOut: EventEmitter = new EventEmitter(); /** * @internal */ @ContentChildren(AgmInfoWindow) infoWindow: QueryList = new QueryList(); private _markerAddedToManger: boolean = false; private _id: string; private _observableSubscriptions: Subscription[] = []; constructor(private _markerManager: MarkerManager) { this._id = (markerId++).toString(); } /* @internal */ ngAfterContentInit() { this.handleInfoWindowUpdate(); this.infoWindow.changes.subscribe(() => this.handleInfoWindowUpdate()); } private handleInfoWindowUpdate() { if (this.infoWindow.length > 1) { throw new Error('Expected no more than one info window.'); } this.infoWindow.forEach(marker => { marker.hostMarker = this; }); } /** @internal */ ngOnChanges(changes: {[key: string]: SimpleChange}) { if (typeof this.latitude !== 'number' || typeof this.longitude !== 'number') { return; } if (!this._markerAddedToManger) { this._markerManager.addMarker(this); this._markerAddedToManger = true; this._addEventListeners(); return; } if (changes['latitude'] || changes['longitude']) { this._markerManager.updateMarkerPosition(this); } if (changes['title']) { this._markerManager.updateTitle(this); } if (changes['label']) { this._markerManager.updateLabel(this); } if (changes['draggable']) { this._markerManager.updateDraggable(this); } if (changes['iconUrl']) { this._markerManager.updateIcon(this); } if (changes['opacity']) { this._markerManager.updateOpacity(this); } if (changes['visible']) { this._markerManager.updateVisible(this); } if (changes['zIndex']) { this._markerManager.updateZIndex(this); } } private _addEventListeners() { const cs = this._markerManager.createEventObservable('click', this).subscribe(() => { if (this.openInfoWindow) { this.infoWindow.forEach(infoWindow => infoWindow.open()); } this.markerClick.emit(null); }); this._observableSubscriptions.push(cs); const ds = this._markerManager.createEventObservable('dragend', this) .subscribe((e: mapTypes.MouseEvent) => { this.dragEnd.emit({coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}}); }); this._observableSubscriptions.push(ds); const mover = this._markerManager.createEventObservable('mouseover', this) .subscribe((e: mapTypes.MouseEvent) => { this.mouseOver.emit({coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}}); }); this._observableSubscriptions.push(mover); const mout = this._markerManager.createEventObservable('mouseout', this) .subscribe((e: mapTypes.MouseEvent) => { this.mouseOut.emit({coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}}); }); this._observableSubscriptions.push(mout); } /** @internal */ id(): string { return this._id; } /** @internal */ toString(): string { return 'AgmMarker-' + this._id.toString(); } /** @internal */ ngOnDestroy() { this._markerManager.deleteMarker(this); // unsubscribe all registered observable subscriptions this._observableSubscriptions.forEach((s) => s.unsubscribe()); } }