Advertisement
Guest User

somescript.js

a guest
Apr 6th, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function initMap() {
  2.         var countPerLocation = {"3026": 71, "2022": 3, "5010": 2, "2015": 24, "3067": 2, "3086": 3, "3059": 2, "2046": 1, "2019": 6, "5527": 2, "1009": 1, "4087": 1, "4561": 1, "5577": 1, "1006": 27, "4001": 24, "1079": 21, "3056": 1, "4065": 20, "2075": 4, "4047": 12, "5573": 1, "3054": 24, "4018": 2, "3029": 18, "6091": 4, "4539": 2, "4055": 4, "1042": 6, "2013": 1, "4041": 2, "3033": 1, "5081": 1, "2051": 1, "1078": 1, "5070": 1, "5594": 2, "1004": 1, "4044": 1, "3068": 1, "3035": 5};
  3.         var region = 'district';
  4.         maxCount = 0;
  5.         var countPerLocationArray = [];
  6.         for (locationUid in countPerLocation) {
  7.             maxCount = Math.max( maxCount , countPerLocation[locationUid] );
  8.             countPerLocationArray.push(countPerLocation[locationUid]);
  9.         }
  10.  
  11.         var sort_unique = function(arr) {
  12.             if (arr.length === 0) return arr;
  13.             arr = arr.sort(function (a, b) { return a*1 - b*1; });
  14.             var ret = [arr[0]];
  15.             for (var i = 1; i < arr.length; i++) { //Start loop at 1: arr[0] can never be a duplicate
  16.                 if (arr[i-1] !== arr[i]) {
  17.                 ret.push(arr[i]);
  18.                 }
  19.             }
  20.             return ret;
  21.         }
  22.         var uniqueCountPerLocationSortedData = sort_unique(countPerLocationArray);
  23.  
  24.         var geojsonData;
  25.         if( region == 'thana' ){
  26.             geojsonData = allThanaGeojson;
  27.         }else{
  28.             geojsonData = allDistrictGeojson;
  29.         }
  30.  
  31.        
  32.         var legendColors = [ "#FFEE00", "#FFD509", "#FFBC12", "#FFA31B", "#FF8A24", "#FF712D", "#FF5836", "#FF4040", "#EB3636", "#D72D2D", "#C32424", "#AF1B1B", "#9B1212", "#870909", "#730000"];
  33.  
  34.         var calculateDensity = function(count){
  35.             var bucketSize = Math.floor(uniqueCountPerLocationSortedData.length / legendColors.length);
  36.             var uniqueIndex = uniqueCountPerLocationSortedData.indexOf(count);
  37.             //console.log(uniqueIndex);
  38.             var colorIndex = Math.floor(uniqueIndex/bucketSize);
  39.             if(bucketSize === 0)
  40.                 return uniqueIndex;
  41.             else if(colorIndex >= legendColors.length)
  42.                 return legendColors.length-1;
  43.  
  44.             return colorIndex;
  45.         }
  46.  
  47.         $.map(geojsonData['features'], function(feature){
  48.             count = 0;
  49.             if( countPerLocation[feature.properties.uid] ){
  50.                 count = countPerLocation[feature.properties.uid];
  51.             }
  52.             //feature['properties']['density'] = count / maxCount;
  53.             feature['properties']['density'] = calculateDensity(count);
  54.             feature['properties']['count'] = count;
  55.  
  56.         });
  57.  
  58.  
  59.         var map = L.map('map').setView([23.8103, 90.4125], 7);
  60.  
  61.         var tiles = L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  62.              attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
  63.         }).addTo(map);
  64.  
  65.  
  66.         function getColor(d) {
  67.             return legendColors[d];
  68.         }
  69.  
  70.         function style(feature) {
  71.             return {
  72.                 fillColor: getColor(feature.properties.density),
  73.                 weight: 2,
  74.                 opacity: 1,
  75.                 color: "#fffbcf",
  76.                 dashArray: '3',
  77.                 fillOpacity: 0.7
  78.             };
  79.         }
  80.  
  81.         var geojson;
  82.         var info = L.control();
  83.  
  84.         info.onAdd = function (map) {
  85.             this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
  86.             this.update();
  87.             return this._div;
  88.         };
  89.  
  90.        
  91.         info.update = function (props) {
  92.             this._div.innerHTML = '<h4>তথ্য</h4>' +  (props ?
  93.                 '<b>' + props.name + '</b><br />' + props.count + ' '
  94.                 : 'এলাকাভিত্তিক সংখ্যা দেখার জন্য,<br/>ম্যাপের নির্দিষ্ট জায়গা বরাবর মাউস নিয়ে যান');
  95.         };
  96.  
  97.         info.addTo(map);
  98.  
  99.         function highlightFeature(e) {
  100.             var layer = e.target;
  101.  
  102.             layer.setStyle({
  103.                 weight: 2,
  104.                 color: '#666',
  105.                 dashArray: '',
  106.                 fillOpacity: 0.7
  107.             });
  108.  
  109.             if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
  110.                 layer.bringToFront();
  111.             }
  112.             info.update(layer.feature.properties);
  113.         }
  114.  
  115.         function resetHighlight(e) {
  116.             geojson.resetStyle(e.target);
  117.             info.update();
  118.         }
  119.         function zoomToFeature(e) {
  120.             //map.fitBounds(e.target.getBounds());
  121.         }
  122.  
  123.         function onEachFeature(feature, layer) {
  124.             layer.on({
  125.                 mouseover: highlightFeature,
  126.                 mouseout: resetHighlight,
  127.                 click: zoomToFeature
  128.             });
  129.         }
  130.  
  131.         geojson = L.geoJson(geojsonData, {
  132.             style: style,
  133.             onEachFeature: onEachFeature
  134.         }).addTo(map);
  135.  
  136.  
  137.    
  138.         $.map(geojsonData['features'], function(feature){
  139.             if( feature.properties.count == 0 ){
  140.                 return;
  141.             }
  142.             districtInfo = allDistrictInformation[ feature.properties.uid ]
  143.  
  144.             var textLatLng = [ districtInfo.lat , districtInfo.long  ];
  145.             var myTextLabel = L.marker(textLatLng, {
  146.                 icon: L.divIcon({
  147.                     className: 'text-labels',   // Set class for CSS styling
  148.                     html: feature.properties.count
  149.                 }),
  150.                 zIndexOffset: 1000     // Make appear above other map features
  151.             }).addTo(map);
  152.  
  153.         });
  154.    
  155.  
  156.         /*
  157.         var legend = L.control({position: 'bottomright'});
  158.         legend.onAdd = function (map) {
  159.  
  160.             var div = L.DomUtil.create('div', 'info legend'),
  161.                 grades = [0, 10, 20, 50, 100, 200, 500, 1000],
  162.                 labels = [];
  163.  
  164.             // loop through our density intervals and generate a label with a colored square for each interval
  165.             for (var i = 0; i < grades.length; i++) {
  166.                 div.innerHTML +=
  167.                     '<i style="background:' + getColor(grades[i] + 1) + '"></i> ' +
  168.                     grades[i] + (grades[i + 1] ? '&ndash;' + grades[i + 1] + '<br>' : '+');
  169.             }
  170.  
  171.             return div;
  172.         };
  173.  
  174.         legend.addTo(map);
  175.         */
  176.  
  177.  
  178.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement