Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import uuid from 'uuid';
  2. import Location from './location';
  3. import Point from './point';
  4. import polyFetch from 'whatwg-fetch';
  5. import { Promise as ES6Promise } from 'es6-promise-polyfill';
  6. import MapBox from 'mapbox-gl';
  7. import axios from 'axios';
  8. import _ from 'lodash';
  9. import PitchToggle from './pitch_toggle';
  10.  
  11. class Map {
  12.  
  13.   constructor(platform, config) {
  14.     this.id = uuid.v4();
  15.     this.platform = platform;
  16.     this.config = config;
  17.     this.div = 'map';
  18.     this.tileLayers = [];
  19.     this.locationTracking = true;
  20.     this.initialized = false;
  21.     this.circles = {};
  22.     this.firstCircleLayer = null;
  23.     this.floorplans = {};
  24.  
  25.     this.platform.window.axios = axios;
  26.     this.platform.window.Promise = ES6Promise;
  27.     this.initMap();
  28.   }
  29.  
  30.   remove() {
  31.     this.map.remove();
  32.     this.platform.onMapRemoved(this);
  33.   }
  34.  
  35.   setLocationTracking(enabled) {
  36.     this.locationTracking = enabled;
  37.   }
  38.  
  39.   addFloorPlan(floorPlan) {    
  40.     if (this.initialized) {
  41.       // const id = floorPlan.id;
  42.       const id = 'floorplan';
  43.       const sourceId = 'floorplan-source';
  44.       const source = this.map.getSource(sourceId);
  45.       if (source) {
  46.         this.map.removeSource(sourceId);
  47.         this.map.removeLayer(id);
  48.       }
  49.      
  50.       const c = floorPlan.corners;
  51.  
  52.       const options = {
  53.         type: 'image',
  54.         url: floorPlan.imageUrl,
  55.         coordinates: [c[3], c[2], c[0], c[1]]
  56.       };
  57.  
  58.       this.map.addSource(sourceId, options);
  59.  
  60.       this.map.addLayer({
  61.         id: id,
  62.         source: sourceId,
  63.         type: 'raster'
  64.       });
  65.  
  66.       if (this.firstCircleLayer) {
  67.         console.log('moving layer behind', this.firstCircleLayer);
  68.         this.map.moveLayer(id, this.firstCircleLayer);
  69.       }
  70.     }
  71.   }
  72.  
  73.   getCenter() {
  74.     return Location.fromLatLng(this.map.getCenter());
  75.   }
  76.  
  77.   removeLastTileLayer() {
  78.     const lastLayer = this.tileLayers[this.tileLayers.length - 1];
  79.     this.map.removeLayer(lastLayer);
  80.     this.tileLayers.pop();
  81.   }
  82.  
  83.   setCurrentLocationMarker(location, title) {    
  84.     if (this.currentLocationMarker === undefined) {
  85.       this.currentLocationMarker = this.addMarker(location, {});
  86.       window.koko = this.currentLocationMarker;
  87.       this.map.panTo(location.lngLat);
  88.     } else {
  89.       this.currentLocationMarker.setLngLat(location.lngLat);
  90.     }
  91.   }
  92.  
  93.   geoJsonCircle(center, radiusInKm, points) {
  94.     if(!points) points = 64;
  95.  
  96.     var coords = {
  97.         latitude: center[1],
  98.         longitude: center[0]
  99.     };
  100.  
  101.     var km = radiusInKm;
  102.  
  103.     var ret = [];
  104.     var distanceX = km/(111.320*Math.cos(coords.latitude*Math.PI/180));
  105.     var distanceY = km/110.574;
  106.  
  107.     var theta, x, y;
  108.     for(var i=0; i<points; i++) {
  109.         theta = (i/points)*(2*Math.PI);
  110.         x = distanceX*Math.cos(theta);
  111.         y = distanceY*Math.sin(theta);
  112.  
  113.         ret.push([coords.longitude+x, coords.latitude+y]);
  114.     }
  115.     ret.push(ret[0]);
  116.  
  117.     return {
  118.       "type": "geojson",      
  119.       "data": {
  120.         "type": "FeatureCollection",        
  121.         "features": [{              
  122.           "type": "Feature",
  123.           "properties": {
  124.             "level": 0,
  125.             "height": 0,
  126.             "base_height": 0,
  127.             "color": "green",
  128.             "name": "Geofence"
  129.           },
  130.           "geometry": {
  131.               "type": "Polygon",
  132.               "coordinates": [ret]
  133.           }
  134.         }]
  135.       }
  136.     };
  137.   }
  138.  
  139.   addCircle(id, location, radius, color, fillColor, opacity) {
  140.     if (this.initialized) {
  141.       console.log('adding circle', id);
  142.       const sourceId = 'source-' + id;
  143.       const layerId = 'layer-' + id;
  144.  
  145.       if (!this.firstCircleLayer) {
  146.         this.firstCircleLayer = layerId;
  147.       }
  148.  
  149.       if (this.map.getSource(sourceId)) {
  150.         this.map.removeSource(sourceId);
  151.         this.map.removeLayer(layerId);
  152.       }
  153.  
  154.       const circle = this.geoJsonCircle(location.lngLat, radius / 1000);
  155.      
  156.       const layer = {
  157.         "id": layerId,
  158.         // "type": "fill-extrusion",
  159.         "type": "fill",
  160.         "source": sourceId,
  161.         "layout": {},
  162.         "paint": {          
  163.           "fill-color": fillColor,
  164.           "fill-opacity": opacity,
  165.           // 'fill-extrusion-color': {
  166.           //     // Get the fill-extrusion-color from the source 'color' property.
  167.           //     'property': 'color',
  168.           //     'type': 'identity'
  169.           // },
  170.           // 'fill-extrusion-height': {
  171.           //     // Get fill-extrusion-height from the source 'height' property.
  172.           //     'property': 'height',
  173.           //     'type': 'identity'
  174.           // },
  175.           // 'fill-extrusion-base': {
  176.           //     // Get fill-extrusion-base from the source 'base_height' property.
  177.           //     'property': 'base_height',
  178.           //     'type': 'identity'
  179.           // },
  180.           // // Make extrusions slightly opaque for see through indoor walls.
  181.           // 'fill-extrusion-opacity': 0.5
  182.         }
  183.       };  
  184.      
  185.       this.map.addSource(sourceId, circle);
  186.       this.map.addLayer(layer);
  187.     } else {
  188.       console.log('skipping circle', id);
  189.     }
  190.   }
  191.  
  192.   addMarker(location, options) {
  193.     const defaults = {
  194.       iconSize: [78, 78],
  195.       iconAnchor: [-36, -36],
  196.       iconUrl: 'images/icon_bludot2x.png'      
  197.     };
  198.  
  199.     const settings = _.merge(defaults, options);    
  200.  
  201.     const el = document.createElement('div');
  202.     el.className = 'marker';
  203.     el.style.width = settings.iconSize[0] + 'px';
  204.     el.style.height = settings.iconSize[1] + 'px';
  205.     el.style.backgroundImage = `url(${settings.iconUrl})`;
  206.     el.style.backgroundSize = '100% 100%';
  207.  
  208.     el.addEventListener('click', function() {      
  209.       window.alert(marker.properties.message);
  210.     });
  211.  
  212.     // add marker to map    
  213.  
  214.     const markerOptions = {
  215.       offset: [ settings.iconAnchor[0], settings.iconAnchor[1] ]
  216.     };
  217.    
  218.     const marker = new MapBox.Marker(el, markerOptions)
  219.         .setLngLat(location.lngLat)
  220.         .addTo(this.map);    
  221.  
  222.     return marker;
  223.   }
  224.  
  225.   setView(location, zoom) {        
  226.     this.map.setCenter(location.lngLat);
  227.     if (zoom) {      
  228.       this.map.setZoom(zoom);
  229.     }
  230.   }
  231.  
  232.   viewReset() {
  233.     this.platform.onMapViewReset(this);
  234.   }
  235.  
  236.   onClick(event) {
  237.     console.log('click event', event);
  238.     // const location = Location.fromLatLng(event.lngLat);    
  239.     // this.platform.onMapClick(this, location);
  240.   }
  241.  
  242.   onLoad() {
  243.     console.log('onLoad', this);
  244.     Proximiio.controller.map.initialized = true;
  245.     Proximiio.controller.map.platform.onMapReady(true);
  246.   }
  247.  
  248.   get showZoomControls() {
  249.     return this.config.showZoomControls || true;
  250.   }
  251.  
  252.   get drawEnabled() {
  253.     return this.config.enableDraw || false;
  254.   }
  255.  
  256.   get draggingEnabled() {
  257.     return this.config.dragging || true;
  258.   }
  259.  
  260.   log(message) {
  261.     console.log(message);
  262.   }
  263.  
  264.   initMap() {
  265.     const _this = this;
  266.     this.map = new MapBox.Map({
  267.       container: 'map',
  268.       style: 'https://raw.githubusercontent.com/osm2vectortiles/mapbox-gl-styles/master/styles/bright-v9-cdn.json',
  269.       token: 'no-token',
  270.       maxZoom: 24
  271.     });
  272.    
  273.     this.map.addControl(new MapBox.NavigationControl());
  274.     this.map.on('click', (evt) => {
  275.       _this.onClick(evt);
  276.     });
  277.     this.map.on('load', this.onLoad);
  278.     this.map.addControl(new PitchToggle({minpitchzoom: 11}));
  279.   }
  280.  
  281. }
  282.  
  283. export default Map;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement