forked from sebholstein/angular-google-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marker.ts
235 lines (203 loc) · 6.6 KB
/
marker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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: `
* <agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
* <agm-marker [latitude]="lat" [longitude]="lng" [label]="'M'">
* </agm-marker>
* </agm-map>
* `
* })
* ```
*/
@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;
/**
* Data associated with this marker
*/
@Input() data: any;
/**
* 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;
/**
* If true, the marker can be clicked. Default value is true.
*/
// tslint:disable-next-line:no-input-rename
@Input('markerClickable') clickable: boolean = true;
/**
* This event emitter gets emitted when the user clicks on the marker.
*/
@Output() markerClick: EventEmitter<void> = new EventEmitter<void>();
/**
* This event is fired when the user stops dragging the marker.
*/
@Output() dragEnd: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
/**
* This event is fired when the user mouses over the marker.
*/
@Output() mouseOver: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
/**
* This event is fired when the user mouses outside the marker.
*/
@Output() mouseOut: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
/**
* @internal
*/
@ContentChildren(AgmInfoWindow) infoWindow: QueryList<AgmInfoWindow> = new QueryList<AgmInfoWindow>();
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);
}
if (changes['clickable']) {
this._markerManager.updateClickable(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<mapTypes.MouseEvent>('dragend', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.dragEnd.emit(<MouseEvent>{coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
this._observableSubscriptions.push(ds);
const mover =
this._markerManager.createEventObservable<mapTypes.MouseEvent>('mouseover', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.mouseOver.emit(<MouseEvent>{data: this.data, marker: this, coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
this._observableSubscriptions.push(mover);
const mout =
this._markerManager.createEventObservable<mapTypes.MouseEvent>('mouseout', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.mouseOut.emit(<MouseEvent>{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());
}
}