leaflet-map.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. window.SONATA_CONFIG.USE_ICHECK = true;
  2. var map,
  3. defaultCoords,
  4. drawnItems,
  5. marker,
  6. loc,
  7. dataValue;
  8. $(document).ready(function () {
  9. defaultCoords = new L.latLng(_MAP_LATITUDE, _MAP_LONGITUDE);
  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. if ($(tab_id).hasClass('active')) {
  46. map._onResize();
  47. } else {
  48. setTimeout(checkForChanges, 500);
  49. }
  50. }
  51. function getDataCoords() {
  52. var jsonParseData = {};
  53. if ($('[name$="[extraData]"]').val()) {
  54. dataValue = $('[name$="[extraData]"]').val();
  55. }
  56. if (dataValue) {
  57. jsonParseData = JSON.parse(dataValue);
  58. }
  59. loc = defaultCoords;
  60. if (jsonParseData.lat && jsonParseData.lng) {
  61. loc = new L.latLng(jsonParseData.lat, jsonParseData.lng);
  62. }
  63. return loc;
  64. }
  65. function drawMarker() {
  66. if (marker) {
  67. drawnItems.removeLayer(marker);
  68. }
  69. map.panTo(loc);
  70. marker = L.marker([loc.lat, loc.lng]);
  71. drawnItems.addLayer(marker);
  72. }
  73. function getZoomLevel() {
  74. var jsonParseData = {};
  75. var zoomLevel = 18;
  76. if ($('[name$="[extraData]"]').val()) {
  77. dataValue = $('[name$="[extraData]"]').val();
  78. }
  79. if (dataValue) {
  80. jsonParseData = JSON.parse(dataValue);
  81. }
  82. if (jsonParseData.zoom) {
  83. zoomLevel = jsonParseData.zoom;
  84. }
  85. return zoomLevel;
  86. }