addmap.js 1.7 KB

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