addmap.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function viewMap() {
  2. var map = L.map('mapid').setView([43.797, 11.2400], 11);
  3. // Set up the OSM layer
  4. L.tileLayer(
  5. 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
  6. ).addTo(map);
  7. $.getJSON('/api/rapporti/get', function(data) {
  8. for(var i in data.rapporti) {
  9. var p = data.rapporti[i]
  10. L.circle([p.lat, p.lng], {
  11. color: p.colore,
  12. fillColor:p.colore,
  13. fillOpacity: 0.2,
  14. opacity: 0.7,
  15. radius: p.radius,
  16. }).addTo(map)
  17. }
  18. })
  19. function addPointToDb(dati) {
  20. $.post('/api/rapporti/add', dati
  21. , function(p) {
  22. console.log(p)
  23. L.circle([p.lat, p.lng], {
  24. color: p.colore,
  25. fillColor:p.colore,
  26. fillOpacity: 0.5,
  27. radius: p.radius,
  28. }).addTo(map)
  29. })
  30. }
  31. function onFormSubmit() {
  32. addPointToDb({
  33. lat: $('#pointform input[name=lat]').val(),
  34. lng: $('#pointform input[name=lng]').val(),
  35. stabilita: $('#pointform select[name=stabilita]').val(),
  36. comprensibile: $('#pointform select[name=comprensibile]').val(),
  37. })
  38. $('#pointform').addClass('hidden')
  39. return false;
  40. }
  41. function onMapClick(e) {
  42. console.log('click', e)
  43. console.log(e.latlng)
  44. var stabilita = 3
  45. console.log(map.getZoom())
  46. if(map.getZoom() > 15) {
  47. stabilita = 19 - map.getZoom()
  48. }
  49. console.log('st', stabilita)
  50. $('#pointform input[name=lat]').val(e.latlng.lat)
  51. $('#pointform input[name=lng]').val(e.latlng.lng)
  52. $('#pointform select[name=stabilita]').val(stabilita)
  53. $('#pointform').removeClass('hidden')
  54. // TODO: aggiungi punto temporaneo, ma poi va rimosso
  55. }
  56. map.on('click', onMapClick)
  57. $('#addPoint').on('click', onFormSubmit)
  58. }
  59. jQuery(function($) {
  60. $('#mapid').css('height', $(window).height() - 200)
  61. viewMap()
  62. })