Guest User

Untitled

a guest
Jan 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import { injectable } from 'inversify';
  2. import { IMapService } from './imap.service';
  3. import { Category } from '~/services/categories';
  4.  
  5. import { Tip } from '~/services/tips';
  6.  
  7.  
  8. @injectable()
  9. export class MapService implements IMapService {
  10.  
  11. private initializedPromise: Promise<void>;
  12.  
  13. constructor () {
  14. this.initialize();
  15. }
  16.  
  17. private loadMapScripts(): void {
  18. const scriptTag = document.createElement('script');
  19. scriptTag.src = '//www.bing.com/api/maps/mapcontrol?callback=OnLoadMaps';
  20. scriptTag.id = 'scriptBingMaps';
  21. document.head.appendChild(scriptTag);
  22. }
  23.  
  24. private initialize(): Promise<void> {
  25. if (!this.initializedPromise) {
  26. this.initializedPromise = new Promise((resolve) => {
  27. (window as any).OnLoadMaps = () => {
  28. resolve();
  29. };
  30. this.loadMapScripts();
  31. });
  32. }
  33.  
  34. return this.initializedPromise;
  35. }
  36.  
  37. public async createMap(mapElement: HTMLElement, coordinates: {lat: number, lng: number}): Promise<any> {
  38. await this.initialize();
  39.  
  40. return new (window as any).Microsoft.Maps.Map(mapElement, {
  41. credentials: 'ApbCxtQryYLUNrnVHSPNiw6tYYw8rXzKn72PRKsmyImrZROg2S6bxBFvRdSIppNW',
  42. center: (window as any).Microsoft.Maps.Location(coordinates.lat, coordinates.lng)
  43. });
  44. }
  45.  
  46. public addPinsFromCategory(map: any, category: Category): void {
  47. category.tips.forEach(tip => {
  48. const pin = new (window as any).Microsoft.Maps.Pushpin({latitude: tip.latitude, longitude: tip.longitude}, null);
  49. map.entities.push(pin);
  50. });
  51. }
  52. }
Add Comment
Please, Sign In to add comment