Guest User

Untitled

a guest
Dec 4th, 2017
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * options:
  3.  * - mapOptions: options to pass to google Map constructor
  4.  * - enableClustering: boolean whether to cluster markers or not, defaults to true
  5.  * - clusterOptions: options to pass to MarkerClusterer constructor
  6. */
  7. function MapWrapper(divID, options) {
  8.     'use strict';
  9.     options = options || {};
  10.  
  11.     var _map = createMap(divID, options.mapOptions),
  12.         _initialized = false,
  13.         _clusterer = createClusterer(_map, options.clusterOptions),
  14.         _trafficLayer = new google.maps.TrafficLayer(),
  15.         _clusteringEnabled = options.enableClustering !== false,
  16.         _defaultLayer = new Layer('Default'),
  17.         _pushpinLayer = new Layer('pushpinLayer'),
  18.         _layers = [],
  19.         _layersArray = [],
  20.         _entities = {},
  21.         _entitiesArray = [],
  22.         _infoboxes = {},
  23.         _places = {},
  24.         _highestMarkerZIndex = google.maps.Marker.MAX_ZINDEX;
  25.  
  26.     return {
  27.         addEntity: addEntity,
  28.         addLayer: addLayer,
  29.         addInfobox: addInfobox,
  30.         addEventListener: addEventListener,
  31.         addPlace: addPlace,
  32.         clearLayer: clearLayer,
  33.         cluster: cluster,
  34.         draw: draw,
  35.         getEntity: getEntity,
  36.         getLayer: getLayer,
  37.         getMap: getMap,
  38.         showDefaultLayer: showDefaultLayer,
  39.         hideDefaultLayer: hideDefaultLayer,
  40.         showTrafficLayer: showTrafficLayer,
  41.         hideTrafficLayer: hideTrafficLayer,
  42.         showEntities: showEntities,
  43.         hideEntities: hideEntities,
  44.         removeEntity: removeEntity,
  45.         removePlace: removePlace,
  46.         clearPlaces: clearPlaces,
  47.         uncluster: uncluster,
  48.         zoom: zoom,
  49.         zoomToLayer: zoomToLayer,
  50.         zoomToPlaces: zoomToPlaces,
  51.         zoomToPlace: zoomToPlace,
  52.         moveEntityToFront: moveEntityToFront,
  53.         moveLayerToFront: moveLayerToFront
  54.     };
  55.  
  56.     //Add entity to a specific layer, or the default layer if 'layerId' is undefined or the layer doesn't exist
  57.     function addEntity(entity, layerId) {
  58.  
  59.         var layer = _layers[layerId];
  60.  
  61.         if (layer)
  62.             layer.add(entity);
  63.         else {
  64.             _defaultLayer.add(entity);
  65.             layer = _defaultLayer;
  66.         }
  67.         entity.layer = layer;
  68.         _entities[entity.id] = entity;
  69.         _entitiesArray.push(entity);
  70.  
  71.     }
  72.     function addLayer(layer) {
  73.         _layers[layer.id] = layer;
  74.         _layersArray.push(layer);
  75.     }
  76.     function addInfobox(id, infobox) {
  77.         _infoboxes[id] = infobox;
  78.         infobox.map = _map;
  79.     }
  80.     function addEventListener(eventName, callback) {
  81.         _map.addEventListener(eventName, callback);
  82.     }
  83.     function createMap(divID, options) {
  84.  
  85.         options = options || {
  86.             backgroundColor: '#f9f9f9',
  87.             center: { lat: 50, lng: 3 },
  88.             disableDefaultUI: true,
  89.             minZoom: 2,
  90.             mapTypeControl: true,
  91.             mapTypeControlOptions: {
  92.                 position: google.maps.ControlPosition.TOP_RIGHT
  93.             },
  94.             noClear: true,
  95.             streetViewControl: true,
  96.             streetViewControlOptions: {
  97.                 position: google.maps.ControlPosition.RIGHT_BOTTOM
  98.             },
  99.             zoom: 5,
  100.         };
  101.  
  102.         return new google.maps.Map(document.getElementById(divID), options);
  103.     }
  104.     function createClusterer(map, options) {
  105.  
  106.         options = options || {
  107.             averageCenter: true,
  108.             imagePath: '/Content/Images/Mobile/Map/m',
  109.             minimumClusterSize: 10,
  110.             zoomOnClick: true,
  111.         };
  112.  
  113.         return new MarkerClusterer(map, [], options);
  114.     }
  115.     function draw() {
  116.  
  117.         if (_clusteringEnabled) {
  118.  
  119.             var googleEntities = _entitiesArray.filter(function (entity) {
  120.                 if (entity.visible)
  121.                     return entity;
  122.             }).map(function (entity) {
  123.                 entity.clusterer = _clusterer;
  124.                 return entity.googleEntity;
  125.             });
  126.  
  127.             _clusterer.addMarkers(googleEntities, !_initialized);
  128.  
  129.             _layersArray.forEach(function (layer) {
  130.                 layer.clusterer = _clusterer;
  131.             });
  132.         }
  133.         else {
  134.             _entitiesArray.forEach(function (entity) {
  135.                 entity.googleEntity.setMap(_map);
  136.             });
  137.         }
  138.  
  139.         _map.addListener('idle', function () {
  140.             _initialized = true;
  141.         });
  142.  
  143.         //Defaultlayer isn't part of any cluster
  144.         _defaultLayer.draw(_map);
  145.         zoom();
  146.     }
  147.     function clearLayer(layer) {
  148.         layer.entities.forEach(function (entity) {
  149.             //removes the entity from the map, not from _entities or _entitiesArray
  150.             entity.remove();
  151.             if (entity.radiusCircle) {
  152.                 entity.radiusCircle.setMap(null);
  153.             }
  154.             var index = _entitiesArray.indexOf(entity);
  155.             _entitiesArray.splice(index, 1);
  156.             delete _entities[entity.id];
  157.         });
  158.         layer.entities = [];
  159.     }
  160.     function cluster() {
  161.         if (_clusteringEnabled)
  162.             return;
  163.         _clusteringEnabled = true;
  164.         _entitiesArray.forEach(function (entity) {
  165.             entity.clusterer = _clusterer;
  166.             entity.remove();
  167.         });
  168.         _layersArray.forEach(function (layer) {
  169.             layer.clusterer = _clusterer;
  170.         });
  171.         this.draw();
  172.     }
  173.     function getEntity(id) {
  174.         return _entities[id] || null;
  175.     }
  176.     function getLayer(id) {
  177.         return _layers[id] || null;
  178.     }
  179.     function getMap() {
  180.         return _map;
  181.     }
  182.     function showDefaultLayer() {
  183.         _defaultLayer.show();
  184.     }
  185.     function hideDefaultLayer() {
  186.         _defaultLayer.hide();
  187.     }
  188.     function showTrafficLayer() {
  189.         _trafficLayer.setMap(_map);
  190.     }
  191.     function hideTrafficLayer() {
  192.         _trafficLayer.setMap(null);
  193.     }
  194.     //This won't make entities show up inside an invisible layer
  195.     function showEntities() {
  196.  
  197.         if (_clusteringEnabled) {
  198.  
  199.             _entitiesArray.forEach(function (entity) {
  200.                 entity.show(false);
  201.             });
  202.             var visibleEntities = _entitiesArray.filter(function (entity) {
  203.                 return entity.visible;
  204.             }).map(function (entity) {
  205.                 return entity.googleEntity;
  206.             });
  207.  
  208.             _clusterer.addMarkers(visibleEntities, !_initialized);
  209.         }
  210.         else {
  211.             _entitiesArray.forEach(function (entity) {
  212.                 entity.show();
  213.             });
  214.         }
  215.     }
  216.     function hideEntities() {
  217.  
  218.         if (_clusteringEnabled) {
  219.             _clusterer.clearMarkers();
  220.             _entitiesArray.forEach(function (entity) {
  221.                 entity.hide(false);
  222.             });
  223.         }
  224.         else {
  225.             _entitiesArray.forEach(function (entity) {
  226.                 entity.hide();
  227.             });
  228.         }
  229.     }
  230.     function removeEntity(id) {
  231.         var entity = getEntity(id);
  232.         if (entity === null) {
  233.             return;
  234.         }
  235.         //removes the entity from the map, not from _entities or _entitiesArray
  236.         entity.remove();
  237.         if (entity.radiusCircle) {
  238.             entity.radiusCircle.setMap(null);
  239.         }
  240.         //remove from the layer
  241.         entity.layer.remove(entity);
  242.         var index = _entitiesArray.indexOf(entity);
  243.         _entitiesArray.splice(index, 1);
  244.         delete _entities[id];
  245.     }
  246.     function uncluster() {
  247.         if (!_clusteringEnabled)
  248.             return;
  249.         _clusteringEnabled = false;
  250.         _clusterer.clearMarkers();
  251.         this.draw();
  252.         _entitiesArray.forEach(function (entity) {
  253.             entity.clusterer = null;
  254.         });
  255.         _layersArray.forEach(function (layer) {
  256.             layer.clusterer = null;
  257.         });
  258.     }
  259.     function zoom(id) {
  260.  
  261.         if (id) {
  262.             var entity = _entities[id];
  263.             zoom(entity.latitude, entity.longitude, 16);
  264.             return;
  265.         }
  266.  
  267.         var visibleEntities = _entitiesArray.filter(function (entity) {
  268.             return entity.googleEntity && entity.visible;
  269.         });
  270.  
  271.         if (visibleEntities.length > 1) {
  272.  
  273.             var latLngBounds = new google.maps.LatLngBounds();
  274.             visibleEntities.forEach(function (entity) {
  275.                 latLngBounds.extend(entity.location);
  276.             });
  277.             _map.fitBounds(latLngBounds);
  278.         }
  279.         else if (visibleEntities.length == 1) {
  280.             zoom(visibleEntities[0].latitude, visibleEntities[0].longitude, 16);
  281.         }
  282.         else
  283.             zoom();
  284.  
  285.         function zoom(latitude, longitude, zoomLevel) {
  286.  
  287.             if (!(latitude || longitude)) {
  288.                 latitude = 50;
  289.                 longitude = 3;
  290.                 zoomLevel = 4;
  291.             }
  292.             _map.setCenter({ lat: latitude, lng: longitude });
  293.             _map.setZoom(zoomLevel);
  294.         }
  295.     }
  296.     function zoomToLayer(layer) {
  297.  
  298.         if (layer.entities.length === 0) {
  299.             _map.setCenter({ lat: 50, lng: 3 });
  300.             _map.setZoom(5);
  301.             return;
  302.         }
  303.  
  304.         if (layer.entities.length === 1) {
  305.             var entity = layer.entities[0];
  306.             _map.setCenter(entity.location);
  307.             _map.setZoom(16);
  308.             return;
  309.         }
  310.  
  311.         var locationArray = layer.entities.reduce(function (locationRect, entity) {
  312.             if (entity.googleEntity && entity.googleEntity.getVisible()) {
  313.                 locationRect.extend(entity.location);
  314.             }
  315.             return locationRect;
  316.         }, new google.maps.LatLngBounds());
  317.  
  318.         _map.fitBounds(locationArray);
  319.  
  320.         if (_map.getZoom() > 16) {
  321.             _map.setZoom(16);
  322.         }
  323.     }
  324.     function addPlace(place) {
  325.         _places[place.id] = place;
  326.         place.googleEntity.setMap(_map);
  327.     }
  328.     function removePlace(id) {
  329.         //calling remove() removes the entity from the map but does not delete it from _places
  330.         _places[id].remove();
  331.         delete _places[id];
  332.     }
  333.     function clearPlaces() {
  334.         for (var key in _places) {
  335.             if (_places.hasOwnProperty(key) && _places[key].googleEntity) {
  336.                 _places[key].remove();
  337.             }
  338.         }
  339.  
  340.         _places = {};
  341.     }
  342.     function zoomToPlaces() {
  343.         var anyVisible = false;
  344.  
  345.         var locationRectangle = new google.maps.LatLngBounds();
  346.         for (var key in _places) {
  347.             if (_places.hasOwnProperty(key) && _places[key].googleEntity && _places[key].googleEntity.getVisible()) {
  348.                 anyVisible = true;
  349.                 var googleEntity = _places[key].googleEntity;
  350.                 var bounds = googleEntity.getBounds();
  351.                 locationRectangle.extend(bounds.getNorthEast());
  352.                 locationRectangle.extend(bounds.getSouthWest());
  353.             }
  354.         }
  355.  
  356.         if (anyVisible) {
  357.             _map.fitBounds(locationRectangle);
  358.         }
  359.         else {
  360.             _map.setCenter({ lat: 50, lng: 3 });
  361.             _map.setZoom(4);
  362.         }
  363.     }
  364.     function zoomToPlace(id) {
  365.         var place = _places[id];
  366.         _map.fitBounds(place.googleEntity.getBounds());
  367.     }
  368.     function moveEntityToFront(id) {
  369.         var entity = getEntity(id);
  370.         if (entity == null) {
  371.             return;
  372.         }
  373.         _highestMarkerZIndex++;
  374.         entity.googleEntity.setZIndex(_highestMarkerZIndex);
  375.     }
  376.     function moveLayerToFront(layerId) {
  377.         var layer = getLayer(layerId);
  378.         _highestMarkerZIndex++;
  379.         layer.entities.forEach(function (entity) {
  380.             entity.googleEntity.setZIndex(_highestMarkerZIndex);
  381.         });
  382.     }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment