UNPKG

13.5 kBJavaScriptView Raw
1"use strict";
2var core_1 = require('@angular/core');
3var google_maps_api_wrapper_1 = require('../services/google-maps-api-wrapper');
4var circle_manager_1 = require('../services/managers/circle-manager');
5var info_window_manager_1 = require('../services/managers/info-window-manager');
6var marker_manager_1 = require('../services/managers/marker-manager');
7var polygon_manager_1 = require('../services/managers/polygon-manager');
8var polyline_manager_1 = require('../services/managers/polyline-manager');
9var kml_layer_manager_1 = require('./../services/managers/kml-layer-manager');
10/**
11 * SebMGoogleMap renders a Google Map.
12 * **Important note**: To be able see a map in the browser, you have to define a height for the CSS
13 * class `sebm-google-map-container`.
14 *
15 * ### Example
16 * ```typescript
17 * import { Component } from '@angular/core';
18 * import { SebmGoogleMap } from 'angular2-google-maps/core';
19 *
20 * @Component({
21 * selector: 'my-map-cmp',
22 * directives: [SebmGoogleMap],
23 * styles: [`
24 * .sebm-google-map-container {
25 * height: 300px;
26 * }
27 * `],
28 * template: `
29 * <sebm-google-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
30 * </sebm-google-map>
31 * `
32 * })
33 * ```
34 */
35var SebmGoogleMap = (function () {
36 function SebmGoogleMap(_elem, _mapsWrapper) {
37 this._elem = _elem;
38 this._mapsWrapper = _mapsWrapper;
39 /**
40 * The longitude that defines the center of the map.
41 */
42 this.longitude = 0;
43 /**
44 * The latitude that defines the center of the map.
45 */
46 this.latitude = 0;
47 /**
48 * The zoom level of the map. The default zoom level is 8.
49 */
50 this.zoom = 8;
51 /**
52 * Enables/disables if map is draggable.
53 */
54 this.draggable = true;
55 /**
56 * Enables/disables zoom and center on double click. Enabled by default.
57 */
58 this.disableDoubleClickZoom = false;
59 /**
60 * Enables/disables all default UI of the Google map. Please note: When the map is created, this
61 * value cannot get updated.
62 */
63 this.disableDefaultUI = false;
64 /**
65 * If false, disables scrollwheel zooming on the map. The scrollwheel is enabled by default.
66 */
67 this.scrollwheel = true;
68 /**
69 * If false, prevents the map from being controlled by the keyboard. Keyboard shortcuts are
70 * enabled by default.
71 */
72 this.keyboardShortcuts = true;
73 /**
74 * The enabled/disabled state of the Zoom control.
75 */
76 this.zoomControl = true;
77 /**
78 * Styles to apply to each of the default map types. Note that for Satellite/Hybrid and Terrain
79 * modes, these styles will only apply to labels and geometry.
80 */
81 this.styles = [];
82 /**
83 * When true and the latitude and/or longitude values changes, the Google Maps panTo method is
84 * used to
85 * center the map. See: https://developers.google.com/maps/documentation/javascript/reference#Map
86 */
87 this.usePanning = false;
88 /**
89 * The initial enabled/disabled state of the Street View Pegman control.
90 * This control is part of the default UI, and should be set to false when displaying a map type
91 * on which the Street View road overlay should not appear (e.g. a non-Earth map type).
92 */
93 this.streetViewControl = true;
94 /**
95 * Sets the viewport to contain the given bounds.
96 */
97 this.fitBounds = null;
98 /**
99 * The initial enabled/disabled state of the Scale control. This is disabled by default.
100 */
101 this.scaleControl = false;
102 /**
103 * The initial enabled/disabled state of the Map type control.
104 */
105 this.mapTypeControl = false;
106 this._observableSubscriptions = [];
107 /**
108 * This event emitter gets emitted when the user clicks on the map (but not when they click on a
109 * marker or infoWindow).
110 */
111 this.mapClick = new core_1.EventEmitter();
112 /**
113 * This event emitter gets emitted when the user right-clicks on the map (but not when they click
114 * on a marker or infoWindow).
115 */
116 this.mapRightClick = new core_1.EventEmitter();
117 /**
118 * This event emitter gets emitted when the user double-clicks on the map (but not when they click
119 * on a marker or infoWindow).
120 */
121 this.mapDblClick = new core_1.EventEmitter();
122 /**
123 * This event emitter is fired when the map center changes.
124 */
125 this.centerChange = new core_1.EventEmitter();
126 /**
127 * This event is fired when the viewport bounds have changed.
128 */
129 this.boundsChange = new core_1.EventEmitter();
130 /**
131 * This event is fired when the map becomes idle after panning or zooming.
132 */
133 this.idle = new core_1.EventEmitter();
134 /**
135 * This event is fired when the zoom level has changed.
136 */
137 this.zoomChange = new core_1.EventEmitter();
138 }
139 /** @internal */
140 SebmGoogleMap.prototype.ngOnInit = function () {
141 // todo: this should be solved with a new component and a viewChild decorator
142 var container = this._elem.nativeElement.querySelector('.sebm-google-map-container-inner');
143 this._initMapInstance(container);
144 };
145 SebmGoogleMap.prototype._initMapInstance = function (el) {
146 this._mapsWrapper.createMap(el, {
147 center: { lat: this.latitude || 0, lng: this.longitude || 0 },
148 zoom: this.zoom,
149 minZoom: this.minZoom,
150 maxZoom: this.maxZoom,
151 disableDefaultUI: this.disableDefaultUI,
152 backgroundColor: this.backgroundColor,
153 draggable: this.draggable,
154 draggableCursor: this.draggableCursor,
155 draggingCursor: this.draggingCursor,
156 keyboardShortcuts: this.keyboardShortcuts,
157 zoomControl: this.zoomControl,
158 styles: this.styles,
159 streetViewControl: this.streetViewControl,
160 scaleControl: this.scaleControl,
161 mapTypeControl: this.mapTypeControl
162 });
163 // register event listeners
164 this._handleMapCenterChange();
165 this._handleMapZoomChange();
166 this._handleMapMouseEvents();
167 this._handleBoundsChange();
168 this._handleIdleEvent();
169 };
170 /** @internal */
171 SebmGoogleMap.prototype.ngOnDestroy = function () {
172 // unsubscribe all registered observable subscriptions
173 this._observableSubscriptions.forEach(function (s) { return s.unsubscribe(); });
174 };
175 /* @internal */
176 SebmGoogleMap.prototype.ngOnChanges = function (changes) {
177 this._updateMapOptionsChanges(changes);
178 this._updatePosition(changes);
179 };
180 SebmGoogleMap.prototype._updateMapOptionsChanges = function (changes) {
181 var options = {};
182 var optionKeys = Object.keys(changes).filter(function (k) { return SebmGoogleMap._mapOptionsAttributes.indexOf(k) !== -1; });
183 optionKeys.forEach(function (k) { options[k] = changes[k].currentValue; });
184 this._mapsWrapper.setMapOptions(options);
185 };
186 /**
187 * Triggers a resize event on the google map instance.
188 * Returns a promise that gets resolved after the event was triggered.
189 */
190 SebmGoogleMap.prototype.triggerResize = function () {
191 var _this = this;
192 // Note: When we would trigger the resize event and show the map in the same turn (which is a
193 // common case for triggering a resize event), then the resize event would not
194 // work (to show the map), so we trigger the event in a timeout.
195 return new Promise(function (resolve) {
196 setTimeout(function () { return _this._mapsWrapper.triggerMapEvent('resize').then(function () { return resolve(); }); });
197 });
198 };
199 SebmGoogleMap.prototype._updatePosition = function (changes) {
200 if (changes['latitude'] == null && changes['longitude'] == null &&
201 changes['fitBounds'] == null) {
202 // no position update needed
203 return;
204 }
205 // we prefer fitBounds in changes
206 if (changes['fitBounds'] && this.fitBounds != null) {
207 this._fitBounds();
208 return;
209 }
210 if (typeof this.latitude !== 'number' || typeof this.longitude !== 'number') {
211 return;
212 }
213 var newCenter = {
214 lat: this.latitude,
215 lng: this.longitude,
216 };
217 if (this.usePanning) {
218 this._mapsWrapper.panTo(newCenter);
219 }
220 else {
221 this._mapsWrapper.setCenter(newCenter);
222 }
223 };
224 SebmGoogleMap.prototype._fitBounds = function () {
225 if (this.usePanning) {
226 this._mapsWrapper.panToBounds(this.fitBounds);
227 return;
228 }
229 this._mapsWrapper.fitBounds(this.fitBounds);
230 };
231 SebmGoogleMap.prototype._handleMapCenterChange = function () {
232 var _this = this;
233 var s = this._mapsWrapper.subscribeToMapEvent('center_changed').subscribe(function () {
234 _this._mapsWrapper.getCenter().then(function (center) {
235 _this.latitude = center.lat();
236 _this.longitude = center.lng();
237 _this.centerChange.emit({ lat: _this.latitude, lng: _this.longitude });
238 });
239 });
240 this._observableSubscriptions.push(s);
241 };
242 SebmGoogleMap.prototype._handleBoundsChange = function () {
243 var _this = this;
244 var s = this._mapsWrapper.subscribeToMapEvent('bounds_changed').subscribe(function () {
245 _this._mapsWrapper.getBounds().then(function (bounds) { _this.boundsChange.emit(bounds); });
246 });
247 this._observableSubscriptions.push(s);
248 };
249 SebmGoogleMap.prototype._handleMapZoomChange = function () {
250 var _this = this;
251 var s = this._mapsWrapper.subscribeToMapEvent('zoom_changed').subscribe(function () {
252 _this._mapsWrapper.getZoom().then(function (z) {
253 _this.zoom = z;
254 _this.zoomChange.emit(z);
255 });
256 });
257 this._observableSubscriptions.push(s);
258 };
259 SebmGoogleMap.prototype._handleIdleEvent = function () {
260 var _this = this;
261 var s = this._mapsWrapper.subscribeToMapEvent('idle').subscribe(function () { _this.idle.emit(void 0); });
262 this._observableSubscriptions.push(s);
263 };
264 SebmGoogleMap.prototype._handleMapMouseEvents = function () {
265 var _this = this;
266 var events = [
267 { name: 'click', emitter: this.mapClick },
268 { name: 'rightclick', emitter: this.mapRightClick },
269 ];
270 events.forEach(function (e) {
271 var s = _this._mapsWrapper.subscribeToMapEvent(e.name).subscribe(function (event) {
272 var value = { coords: { lat: event.latLng.lat(), lng: event.latLng.lng() } };
273 e.emitter.emit(value);
274 });
275 _this._observableSubscriptions.push(s);
276 });
277 };
278 /**
279 * Map option attributes that can change over time
280 */
281 SebmGoogleMap._mapOptionsAttributes = [
282 'disableDoubleClickZoom', 'scrollwheel', 'draggable', 'draggableCursor', 'draggingCursor',
283 'keyboardShortcuts', 'zoomControl', 'styles', 'streetViewControl', 'zoom', 'mapTypeControl',
284 'minZoom', 'maxZoom'
285 ];
286 SebmGoogleMap.decorators = [
287 { type: core_1.Component, args: [{
288 selector: 'sebm-google-map',
289 providers: [
290 google_maps_api_wrapper_1.GoogleMapsAPIWrapper, marker_manager_1.MarkerManager, info_window_manager_1.InfoWindowManager, circle_manager_1.CircleManager, polyline_manager_1.PolylineManager,
291 polygon_manager_1.PolygonManager, kml_layer_manager_1.KmlLayerManager
292 ],
293 inputs: [
294 'longitude', 'latitude', 'zoom', 'minZoom', 'maxZoom', 'draggable: mapDraggable',
295 'disableDoubleClickZoom', 'disableDefaultUI', 'scrollwheel', 'backgroundColor', 'draggableCursor',
296 'draggingCursor', 'keyboardShortcuts', 'zoomControl', 'styles', 'usePanning', 'streetViewControl',
297 'fitBounds', 'scaleControl', 'mapTypeControl'
298 ],
299 outputs: [
300 'mapClick', 'mapRightClick', 'mapDblClick', 'centerChange', 'idle', 'boundsChange', 'zoomChange'
301 ],
302 host: { '[class.sebm-google-map-container]': 'true' },
303 styles: ["\n .sebm-google-map-container-inner {\n width: inherit;\n height: inherit;\n }\n .sebm-google-map-content {\n display:none;\n }\n "],
304 template: "\n <div class='sebm-google-map-container-inner'></div>\n <div class='sebm-google-map-content'>\n <ng-content></ng-content>\n </div>\n "
305 },] },
306 ];
307 /** @nocollapse */
308 SebmGoogleMap.ctorParameters = function () { return [
309 { type: core_1.ElementRef, },
310 { type: google_maps_api_wrapper_1.GoogleMapsAPIWrapper, },
311 ]; };
312 return SebmGoogleMap;
313}());
314exports.SebmGoogleMap = SebmGoogleMap;
315//# sourceMappingURL=google-map.js.map
\No newline at end of file