ng2-heremaps
Version:
Here Maps for Angular 6
239 lines • 8.23 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
import { Injectable } from '@angular/core';
import { LazyMapsApiLoader } from '../loaders/lazy-maps-api-loader';
const /** @type {?} */ DefaultCoords = {
latitude: 40.73061,
longitude: -73.935242
};
/**
* @return {?}
*/
function noop() {
// noop
}
/**
* Service responsible to execute arbitrary functions in specific google map context
*/
export class HereMapsManager {
/**
* @param {?} loader
*/
constructor(loader) {
this.loader = loader;
this._maps = new Map();
// check browser location
this.getBrowserLocation().then(noop);
// preload map immediately
this._loadPromise = this.loader.load();
}
/**
* @return {?}
*/
onApiLoad() {
return this.loader.platformReady;
}
/**
* @param {?} options
* @return {?}
*/
createMarker(options) {
return this.loader.platformReady.then(() => {
return new H.map.Marker(/** @type {?} */ (options.position));
});
}
/**
* @param {?} options
* @return {?}
*/
createBubble(options) {
return this.loader.platformReady.then(() => {
return new H.ui.InfoBubble(/** @type {?} */ (options.position));
});
}
/**
* @param {?} origin
* @param {?} destination
* @param {?=} intermediatePoints
* @return {?}
*/
getDirections(origin, destination, intermediatePoints = []) {
return this.loader.platformReady.then(platform => {
const /** @type {?} */ router = platform.getRoutingService();
return new Promise((resolve, reject) => {
const /** @type {?} */ params = {
mode: 'balanced;truck',
representation: 'navigation'
};
const /** @type {?} */ waypoints = [origin, ...intermediatePoints, destination];
waypoints.forEach((waypoint, index) => (params[`waypoint${index}`] = this.generateWaypointParam(waypoint, index === 0 || index === waypoints.length - 1)));
router.calculateRoute(params, (result) => resolve(result), (error) => {
console.error({
message: 'fail to get directions',
error
});
reject(error);
});
});
});
}
/**
* @param {?} el
* @param {?=} options
* @param {?=} controls
* @return {?}
*/
createMap(el, options, controls) {
return this.loader.platformReady.then(platform => {
const /** @type {?} */ defaultLayers = this._defaultLayers || platform.createDefaultLayers();
this._defaultLayers = defaultLayers;
const /** @type {?} */ map = new H.Map(el, defaultLayers.normal.map, options);
const /** @type {?} */ ui = H.ui.UI.createDefault(map, defaultLayers, 'en-US');
ui.setUnitSystem(H.ui.UnitSystem.IMPERIAL);
const /** @type {?} */ mapEvents = new H.mapevents.MapEvents(map);
const /** @type {?} */ behavior = new H.mapevents.Behavior(mapEvents);
if (controls) {
if (!controls.mapTypeControl) {
ui.removeControl('mapsettings');
}
if (!controls.zoomControl) {
ui.removeControl('zoom');
}
if (!controls.scaleControl) {
ui.removeControl('scalebar');
}
if (!controls.streetViewControl && (/** @type {?} */ (H)).PanoramaView) {
ui.removeControl('panorama');
}
if (!controls.scrollwheel) {
behavior.disable(H.mapevents.Behavior.WHEELZOOM);
}
if (!controls.enableDoubleClickZoom) {
behavior.disable(H.mapevents.Behavior.DBLTAPZOOM);
}
if (!controls.draggable) {
behavior.disable(H.mapevents.Behavior.DRAGGING);
}
}
return { map, ui, platform };
});
}
/**
* @param {?} name
* @return {?}
*/
getMap(name) {
return this._loadPromise.then(() => this._maps.get(name));
}
/**
* @param {?} name
* @param {?} map
* @return {?}
*/
addMap(name, map) {
this._maps.set(name, map);
}
/**
* @param {?} name
* @return {?}
*/
removeMap(name) {
return this._maps.delete(name);
}
/**
* @return {?}
*/
getBrowserLocation() {
if (this._browserLocationPromise) {
return this._browserLocationPromise;
}
return (this._browserLocationPromise = new Promise(resolve => {
if (location.protocol === 'https' && navigator.geolocation) {
navigator.geolocation.getCurrentPosition((success) => {
resolve(success.coords);
}, error => {
console.error(error);
if (error.code !== 1) {
console.warn(`Permission is accepted but error encounter with message: ${error.message}`);
}
// if user didn't answer return default
resolve(DefaultCoords);
});
}
else {
// if browser do not support location API return default (NYC)
resolve(DefaultCoords);
}
}));
}
/**
* @param {?=} points
* @return {?}
*/
calculateMapBounds(points = []) {
return this.loader.platformReady.then(_ => {
const /** @type {?} */ bounds = new H.map.Group().getBounds();
if (points && points.length > 1) {
points.forEach(m => {
if (m instanceof H.map.AbstractMarker) {
bounds.mergePoint(m.getPosition());
}
else {
bounds.mergePoint({
lat: (/** @type {?} */ (m)).latitude !== void 0
? (/** @type {?} */ (m)).latitude
: (/** @type {?} */ (m)).lat,
lng: (/** @type {?} */ (m)).longitude !== void 0
? (/** @type {?} */ (m)).longitude
: (/** @type {?} */ (m)).lng
});
}
});
return Promise.resolve(bounds);
}
return Promise.resolve(bounds);
});
}
/**
* @param {?} coordinates
* @param {?=} end
* @return {?}
*/
generateWaypointParam(coordinates, end = false) {
const /** @type {?} */ lat = (/** @type {?} */ (coordinates)).lat || (/** @type {?} */ (coordinates)).latitude || 0;
const /** @type {?} */ lng = (/** @type {?} */ (coordinates)).lng ||
(/** @type {?} */ (coordinates)).longitude ||
(/** @type {?} */ (coordinates)).lon ||
0;
return `geo!${end ? 'stopOver' : 'passThrough'}!${lat},${lng}`;
}
}
HereMapsManager.decorators = [
{ type: Injectable }
];
/** @nocollapse */
HereMapsManager.ctorParameters = () => [
{ type: LazyMapsApiLoader, },
];
function HereMapsManager_tsickle_Closure_declarations() {
/** @type {!Array<{type: !Function, args: (undefined|!Array<?>)}>} */
HereMapsManager.decorators;
/**
* @nocollapse
* @type {function(): !Array<(null|{type: ?, decorators: (undefined|!Array<{type: !Function, args: (undefined|!Array<?>)}>)})>}
*/
HereMapsManager.ctorParameters;
/** @type {?} */
HereMapsManager.prototype._maps;
/** @type {?} */
HereMapsManager.prototype._browserLocationPromise;
/** @type {?} */
HereMapsManager.prototype._defaultLayers;
/** @type {?} */
HereMapsManager.prototype._loadPromise;
/** @type {?} */
HereMapsManager.prototype.loader;
}
//# sourceMappingURL=maps-manager.js.map