addmap.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. tiporadio:$('#pointform select[name=tiporadio]').val(),
  35. comprensibile: $('#pointform select[name=comprensibile]').val()
  36. })
  37. $('#pointform').addClass('hidden')
  38. return false
  39. }
  40. function onMapClick (e) {
  41. var stabilita = 3
  42. if (map.getZoom() > 15) {
  43. stabilita = 19 - map.getZoom()
  44. }
  45. $('#pointform input[name=lat]').val(e.latlng.lat)
  46. $('#pointform input[name=lng]').val(e.latlng.lng)
  47. $('#pointform select[name=stabilita]').val(stabilita)
  48. $('#pointform').removeClass('hidden')
  49. // TODO: aggiungi punto temporaneo, ma poi va rimosso
  50. }
  51. map.on('click', onMapClick)
  52. $('#addPoint').on('click', onFormSubmit)
  53. }
  54. jQuery(function ($) {
  55. $('#mapid').css('height', $(window).height() - 200)
  56. viewMap()
  57. })