Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function initMap() {
- 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};
- var region = 'district';
- maxCount = 0;
- var countPerLocationArray = [];
- for (locationUid in countPerLocation) {
- maxCount = Math.max( maxCount , countPerLocation[locationUid] );
- countPerLocationArray.push(countPerLocation[locationUid]);
- }
- var sort_unique = function(arr) {
- if (arr.length === 0) return arr;
- arr = arr.sort(function (a, b) { return a*1 - b*1; });
- var ret = [arr[0]];
- for (var i = 1; i < arr.length; i++) { //Start loop at 1: arr[0] can never be a duplicate
- if (arr[i-1] !== arr[i]) {
- ret.push(arr[i]);
- }
- }
- return ret;
- }
- var uniqueCountPerLocationSortedData = sort_unique(countPerLocationArray);
- var geojsonData;
- if( region == 'thana' ){
- geojsonData = allThanaGeojson;
- }else{
- geojsonData = allDistrictGeojson;
- }
- var legendColors = [ "#FFEE00", "#FFD509", "#FFBC12", "#FFA31B", "#FF8A24", "#FF712D", "#FF5836", "#FF4040", "#EB3636", "#D72D2D", "#C32424", "#AF1B1B", "#9B1212", "#870909", "#730000"];
- var calculateDensity = function(count){
- var bucketSize = Math.floor(uniqueCountPerLocationSortedData.length / legendColors.length);
- var uniqueIndex = uniqueCountPerLocationSortedData.indexOf(count);
- //console.log(uniqueIndex);
- var colorIndex = Math.floor(uniqueIndex/bucketSize);
- if(bucketSize === 0)
- return uniqueIndex;
- else if(colorIndex >= legendColors.length)
- return legendColors.length-1;
- return colorIndex;
- }
- $.map(geojsonData['features'], function(feature){
- count = 0;
- if( countPerLocation[feature.properties.uid] ){
- count = countPerLocation[feature.properties.uid];
- }
- //feature['properties']['density'] = count / maxCount;
- feature['properties']['density'] = calculateDensity(count);
- feature['properties']['count'] = count;
- });
- var map = L.map('map').setView([23.8103, 90.4125], 7);
- var tiles = L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
- attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
- }).addTo(map);
- function getColor(d) {
- return legendColors[d];
- }
- function style(feature) {
- return {
- fillColor: getColor(feature.properties.density),
- weight: 2,
- opacity: 1,
- color: "#fffbcf",
- dashArray: '3',
- fillOpacity: 0.7
- };
- }
- var geojson;
- var info = L.control();
- info.onAdd = function (map) {
- this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
- this.update();
- return this._div;
- };
- info.update = function (props) {
- this._div.innerHTML = '<h4>তথ্য</h4>' + (props ?
- '<b>' + props.name + '</b><br />' + props.count + ' '
- : 'এলাকাভিত্তিক সংখ্যা দেখার জন্য,<br/>ম্যাপের নির্দিষ্ট জায়গা বরাবর মাউস নিয়ে যান');
- };
- info.addTo(map);
- function highlightFeature(e) {
- var layer = e.target;
- layer.setStyle({
- weight: 2,
- color: '#666',
- dashArray: '',
- fillOpacity: 0.7
- });
- if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
- layer.bringToFront();
- }
- info.update(layer.feature.properties);
- }
- function resetHighlight(e) {
- geojson.resetStyle(e.target);
- info.update();
- }
- function zoomToFeature(e) {
- //map.fitBounds(e.target.getBounds());
- }
- function onEachFeature(feature, layer) {
- layer.on({
- mouseover: highlightFeature,
- mouseout: resetHighlight,
- click: zoomToFeature
- });
- }
- geojson = L.geoJson(geojsonData, {
- style: style,
- onEachFeature: onEachFeature
- }).addTo(map);
- $.map(geojsonData['features'], function(feature){
- if( feature.properties.count == 0 ){
- return;
- }
- districtInfo = allDistrictInformation[ feature.properties.uid ]
- var textLatLng = [ districtInfo.lat , districtInfo.long ];
- var myTextLabel = L.marker(textLatLng, {
- icon: L.divIcon({
- className: 'text-labels', // Set class for CSS styling
- html: feature.properties.count
- }),
- zIndexOffset: 1000 // Make appear above other map features
- }).addTo(map);
- });
- /*
- var legend = L.control({position: 'bottomright'});
- legend.onAdd = function (map) {
- var div = L.DomUtil.create('div', 'info legend'),
- grades = [0, 10, 20, 50, 100, 200, 500, 1000],
- labels = [];
- // loop through our density intervals and generate a label with a colored square for each interval
- for (var i = 0; i < grades.length; i++) {
- div.innerHTML +=
- '<i style="background:' + getColor(grades[i] + 1) + '"></i> ' +
- grades[i] + (grades[i + 1] ? '–' + grades[i + 1] + '<br>' : '+');
- }
- return div;
- };
- legend.addTo(map);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement