ng2-bingmaps
Version:
Angular 2 components for Bing Maps
142 lines (140 loc) • 5.63 kB
JavaScript
/**
* ng2-bingmaps - Angular 2 components for Bing Maps
* @version v0.2.0
* @link https://github.com/youjustgo/ng2-bingmaps
* @license MIT
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { MapsAPILoader } from './maps-api-loader/maps-api-loader';
import * as mapTypes from './bing-maps-types';
import { LazyMapsAPILoaderConfig } from './maps-api-loader/lazy-maps-api-loader';
/**
* Wrapper class that handles the communication with the Bing Maps Javascript
* API v8
*/
export let BingMapsAPIWrapper = class BingMapsAPIWrapper {
constructor(_loader, _zone, _config) {
this._loader = _loader;
this._zone = _zone;
this._config = _config;
this.infoBox = null;
this._mapResolver = null;
this.nativeMap = new Promise((resolve) => { this._mapResolver = resolve; });
}
createMap(el, mapOptions) {
return this._loader.load().then(() => {
// todo other options
let map = new Microsoft.Maps.Map(el, {
credentials: this._config.apiKey,
center: new Microsoft.Maps.Location(mapOptions.center.lat, mapOptions.center.lng),
zoom: mapOptions.zoom,
mapTypeId: mapOptions.mapTypeId
});
this._mapResolver(map);
/**
* Create one infobox which is reused.
*/
this.infoBox = new Promise((resolve) => { this._infoBoxResolver = resolve; });
var infoBox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), {});
this._infoBoxResolver(infoBox);
return map;
});
}
setMapOptions(options) {
this.nativeMap.then((m) => {
m.setOptions({
center: new Microsoft.Maps.Location(options.center.lat, options.center.lng),
zoom: options.zoom,
mapTypeId: options.mapTypeId
});
// todo other options
});
}
/**
* Creates a Bing map marker with the map context
*/
createMarker(options = {}) {
return this.nativeMap.then((map) => {
var loc = new Microsoft.Maps.Location(options.position.lat, options.position.lng);
var pushpin = new Microsoft.Maps.Pushpin(loc);
map.entities.push(pushpin);
return new mapTypes.Marker(map, pushpin);
});
}
getInfoWindow(options) {
return this.nativeMap.then((map) => {
return this.infoBox.then((infoBox) => {
if (typeof options === "undefined" || options == null) {
return new mapTypes.InfoWindow(map, infoBox);
}
else {
var nativeOptions = {
visible: false,
title: options.title,
location: new Microsoft.Maps.Location(options.position.lat, options.position.lng),
description: options.description,
actions: options.actions
};
if (options.height > 0) {
nativeOptions.maxHeight = options.height;
}
if (options.width > 0) {
nativeOptions.maxWidth = options.width;
}
infoBox.setOptions(nativeOptions);
return new mapTypes.InfoWindow(map, infoBox);
}
});
});
}
subscribeToMapEvent(eventName) {
return Observable.create((observer) => {
this.nativeMap.then((m) => {
Microsoft.Maps.Events.addHandler(m, eventName, (e) => {
this._zone.run(() => observer.next(e));
});
});
});
}
setCenter(latLng) {
return this.nativeMap.then((map) => map.setView({
center: new Microsoft.Maps.Location(latLng.lat, latLng.lng)
}));
}
getZoom() {
return this.nativeMap.then((map) => map.getZoom());
}
setZoom(zoom) {
return this.nativeMap.then((map) => map.setView({ zoom: zoom }));
}
getCenter() {
return this.nativeMap.then((map) => {
let center = map.getCenter();
return {
lat: center.latitude,
lng: center.longitude
};
});
}
/**
* Triggers the given event name on the map instance.
*/
triggerMapEvent(eventName) {
return this.nativeMap.then((m) => Microsoft.Maps.Events.invoke(m, eventName, null));
}
};
BingMapsAPIWrapper = __decorate([
Injectable(),
__metadata('design:paramtypes', [MapsAPILoader, NgZone, LazyMapsAPILoaderConfig])
], BingMapsAPIWrapper);
//# sourceMappingURL=bing-maps-api-wrapper.js.map