leaflet-map.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. window.SONATA_CONFIG.USE_ICHECK = false;
  2. var map,
  3. defaultCoords,
  4. drawnItems,
  5. marker,
  6. loc,
  7. dataValue;
  8. $(document).ready(function () {
  9. defaultCoords = new L.latLng(39.791312, -2.6949709); // España
  10. map = new L.Map('map', {zoom: getZoomLevel(), center: getDataCoords()});
  11. drawnItems = L.featureGroup().addTo(map);
  12. drawMarker();
  13. L.control.layers(
  14. {
  15. 'openstreetmap': L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
  16. {
  17. maxZoom: 18,
  18. attribution: '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
  19. }).addTo(map),
  20. "google": L.tileLayer('http://www.google.com.ar/maps/vt?lyrs=y@189&gl=cn&x={x}&y={y}&z={z}',
  21. {
  22. maxZoom: 18,
  23. attribution: 'google'
  24. })
  25. },
  26. {
  27. 'drawlayer': drawnItems
  28. },
  29. {
  30. position: 'topleft',
  31. collapsed: false
  32. }).addTo(map);
  33. map.on('zoomend', function () {
  34. setDataValue();
  35. });
  36. });
  37. // Espera que el tab mostrado tenga la clase CSS 'active' y hace un refresh del mapa
  38. // debido a que al estar oculto en un tab no se muestra con el tamaño correcto
  39. var tab_id;
  40. $('.nav-tabs a').on('click', function() {
  41. tab_id = $(this).attr('href');
  42. checkForChanges();
  43. });
  44. function checkForChanges()
  45. {
  46. if ($(tab_id).hasClass('active')) {
  47. map._onResize();
  48. } else {
  49. setTimeout(checkForChanges, 500);
  50. }
  51. }
  52. function getDataCoords()
  53. {
  54. var jsonParseData = {};
  55. if ($('[name$="[extraData]"]').val()) {
  56. dataValue = $('[name$="[extraData]"]').val();
  57. }
  58. if (dataValue) {
  59. jsonParseData = JSON.parse(dataValue);
  60. }
  61. loc = defaultCoords;
  62. if (jsonParseData.lat && jsonParseData.lng) {
  63. loc = new L.latLng(jsonParseData.lat, jsonParseData.lng);
  64. }
  65. return loc;
  66. }
  67. function drawMarker()
  68. {
  69. if (marker) {
  70. drawnItems.removeLayer(marker);
  71. }
  72. map.panTo(loc);
  73. marker = L.marker([loc.lat, loc.lng]);
  74. drawnItems.addLayer(marker);
  75. }
  76. function getZoomLevel()
  77. {
  78. var jsonParseData = {};
  79. var zoomLevel = 18;
  80. if ($('[name$="[extraData]"]').val()) {
  81. dataValue = $('[name$="[extraData]"]').val();
  82. }
  83. if (dataValue) {
  84. jsonParseData = JSON.parse(dataValue);
  85. }
  86. if (jsonParseData.zoom) {
  87. zoomLevel = jsonParseData.zoom;
  88. }
  89. return zoomLevel;
  90. }