Advertisement
tungSfer

map.js

Apr 7th, 2022
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var iconRed = L.icon({
  2.     iconUrl: './images/red-garbage.png',
  3.     iconSize: [25, 41],
  4. });
  5. var iconGreen = L.icon({
  6.     iconUrl: './images/green-garbage.png',
  7.     iconSize: [25, 41],
  8. });
  9. var iconYellow = L.icon({
  10.     iconUrl: './images/yellow-garbage.png',
  11.     iconSize: [25, 41],
  12. });
  13. var iconVehicleRed = L.icon({
  14.     iconUrl: './images/red-vehicle.png',
  15.     iconSize: [16, 35],
  16. });
  17. var iconVehicleGreen = L.icon({
  18.     iconUrl: './images/green-vehicle.png',
  19.     iconSize: [16, 35],
  20. });
  21.  
  22. // console.log(iconRed);
  23. // console.log(iconGreen);
  24. // console.log(iconYellow);
  25.  
  26. function _getAngle(startx, starty, endx, endy) {
  27.     var tan = 0
  28.     if (endx == startx) {
  29.         tan = 90;
  30.     } else {
  31.         tan = Math.atan(Math.abs((endy - starty) / (endx - startx))) * 180 / Math.PI;
  32.         console.log(tan);
  33.     }
  34.  
  35.     if (endx >= startx && endy >= starty) //First quadrant
  36.     {
  37.         return -tan;
  38.     } else if (endx > startx && endy < starty) //Delta Quadrant
  39.     {
  40.         return tan;
  41.     } else if (endx < startx && endy > starty) //Beta Quadrant
  42.     {
  43.         return tan - 180;
  44.     } else {
  45.         return 180 - tan; //third quadrant
  46.     }
  47. }
  48.  
  49.  
  50. var map = L.map('map').setView([21.037181, 105.833485], 15);
  51.  
  52. L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
  53.     attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  54.     maxZoom: 20,
  55.     id: 'mapbox/streets-v11',
  56.     tileSize: 512,
  57.     zoomOffset: -1,
  58.     accessToken: 'pk.eyJ1IjoidHVuZ2J2NTEyMDFzZiIsImEiOiJjbDFtOXFveGgwaWVlM2psNmtrNWVtbzNqIn0.eV3E0R0knRqWfCCsfeo2Fw'
  59. }).addTo(map);
  60.  
  61. var statBin = {
  62.     "full": iconRed,
  63.     "empty": iconGreen,
  64.     "mid": iconYellow,
  65. };
  66. var statVehicle = {
  67.     "off": iconVehicleRed,
  68.     "on": iconVehicleGreen,
  69. };
  70. var objBin = [];
  71. var objVehicle = [];
  72. var layerGrBin, layerGrVehicle;
  73. var oB = new Object();
  74. var oV = new Object();
  75. var x = null,
  76.     y = null;
  77. setInterval(function() {
  78.     objBin = [];
  79.     $.getJSON('https://demoleaflet.000webhostapp.com/majestic/Dashboard/php/api/post/R_bin.php', {}, (data) => {
  80.         for (let i = 0; i < data.data.length; i++) {
  81.             oB = {
  82.                 lat: data.data[i].lat,
  83.                 long: data.data[i].long,
  84.                 markerBin: L.marker([data.data[i].lat, data.data[i].long]),
  85.             };
  86.             oB.markerBin.setIcon(statBin[data.data[i].statuses]);
  87.             // o.markerBin.addTo(map);
  88.             objBin.push(oB.markerBin);
  89.         }
  90.         console.log(objBin);
  91.     });
  92.     objVehicle = [];
  93.     $.getJSON('https://demoleaflet.000webhostapp.com/majestic/Dashboard/php/api/post/R_vehicle.php', {}, (data) => {
  94.         let body = document.querySelector(".table tbody");
  95.         for (let i = 0; i < data.data.length; i++) {
  96.             oV = {
  97.                 lat: data.data[i].lat,
  98.                 long: data.data[i].long,
  99.                 markerVehicle: L.marker([data.data[i].lat, data.data[i].long]),
  100.             };
  101.             oV.markerVehicle.setIcon(statVehicle[data.data[i].statuses]);
  102.             objVehicle.push(oV.markerVehicle);
  103.         }
  104.     });
  105.     setTimeout(function() {
  106.         console.log(objBin);
  107.         console.log(objVehicle);
  108.         if (layerGrBin) {
  109.             map.removeLayer(layerGrBin);
  110.         }
  111.         if (layerGrVehicle) {
  112.             map.removeLayer(layerGrVehicle);
  113.         }
  114.         layerGrBin = L.layerGroup(objBin).addTo(map);
  115.         layerGrVehicle = L.layerGroup(objVehicle).addTo(map);
  116.     }, 1000);
  117. }, 2000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement