'use strict'; (function() { var module = angular.module('redash.visualization'); module.config(['VisualizationProvider', function(VisualizationProvider) { var renderTemplate = '' + ''; var editTemplate = ''; var defaultOptions = { height: 500, classify: 'none', clusterMarkers: true }; VisualizationProvider.registerVisualization({ type: 'MAP', name: 'Map', renderTemplate: renderTemplate, editorTemplate: editTemplate, defaultOptions: defaultOptions }); } ]); module.directive('mapRenderer', function() { return { restrict: 'E', templateUrl: '/views/visualizations/map.html', link: function($scope, elm, attrs) { $scope.$watch('queryResult && queryResult.getData()', render, true); $scope.$watch('visualization.options', render, true); angular.element(window).on("resize", resize); $scope.$watch('visualization.options.height', resize); var color = d3.scale.category10(); var map = L.map(elm[0].children[0].children[0], {scrollWheelZoom: false}); var mapControls = L.control.layers().addTo(map); var layers = {}; var tileLayer = L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap contributors' }).addTo(map); map.on('focus',function(){ map.on('moveend', getBounds); }); map.on('blur',function(){ map.off('moveend', getBounds); }); // Following line is used to avoid "Couldn't autodetect L.Icon.Default.imagePath" error // https://github.com/Leaflet/Leaflet/issues/766#issuecomment-7741039 L.Icon.Default.imagePath = L.Icon.Default.imagePath || "//api.tiles.mapbox.com/mapbox.js/v2.2.1/images"; function resize() { if (!map) return; map.invalidateSize(false); setBounds(); } function setBounds (){ var b = $scope.visualization.options.bounds; if(b){ map.fitBounds([[b._southWest.lat, b._southWest.lng],[b._northEast.lat, b._northEast.lng]]); } else if (layers){ var allMarkers = _.flatten(_.map(_.values(layers), function(l) { return l.getLayers() })); var group = new L.featureGroup(allMarkers); map.fitBounds(group.getBounds()); } }; var createMarker = function(lat,lon){ if (lat == null || lon == null) return; return L.marker([lat, lon]); }; var heatpoint = function(lat, lon, color){ if (lat == null || lon == null) return; var style = { fillColor:color, fillOpacity:0.9, stroke:false }; return L.circleMarker([lat,lon],style) }; function getBounds() { $scope.visualization.options.bounds = map.getBounds(); } function createDescription(latCol, lonCol, row) { var lat = row[latCol]; var lon = row[lonCol]; var description = '