leaflet-map-widget.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var geocoder;
  2. $(document).ready(function () {
  3. placeholder = 'Buscar...';
  4. geocoder = new google.maps.Geocoder();
  5. map.addControl(new L.Control.Search({
  6. sourceData: googleGeocoding,
  7. formatData: formatJSON,
  8. markerLocation: true,
  9. autoType: true,
  10. autoCollapse: false,
  11. minLength: 3,
  12. position: 'topright',
  13. delayType: 1,
  14. tooltipLimit: -1,
  15. firstTipSubmit: true,
  16. autoResize: false,
  17. collapsed: false,
  18. textPlaceholder: placeholder
  19. }));
  20. $('.search-button').on('click', function () {
  21. if (loc && $('.search-input').val() !== placeholder) {
  22. drawMarker();
  23. setDataValue();
  24. }
  25. });
  26. $('.search-input').on('keypress', function (e) {
  27. if (e.keyCode == 13 && loc) {
  28. e.preventDefault();
  29. e.stopPropagation();
  30. drawMarker();
  31. setDataValue();
  32. }
  33. if (typeof addAddressToExtraData === 'function') {
  34. addAddressToExtraData();
  35. }
  36. });
  37. $('.search-tooltip').on('click', function () {
  38. var tooltip = $(this).children('.search-tip');
  39. var location = '';
  40. if (tooltip) {
  41. location = tooltip.html();
  42. }
  43. if (location !== '') {
  44. $('.search-input').val(location);
  45. $('.search-tooltip').css('display', 'none');
  46. $('.search-button').trigger('click');
  47. }
  48. });
  49. });
  50. function setDataValue() {
  51. var jsonParseData = {};
  52. var dataValue = $('[name$="[extraData]"]').val();
  53. if (dataValue) {
  54. jsonParseData = JSON.parse(dataValue);
  55. }
  56. jsonParseData.lat = marker._latlng.lat;
  57. jsonParseData.lng = marker._latlng.lng;
  58. jsonParseData.zoom = map.getZoom();
  59. var stringifyData = JSON.stringify(jsonParseData);
  60. $('[name$="[extraData]"]').val(stringifyData);
  61. $('[name$="[map]"]').val(stringifyData);
  62. // si existe la funcion la llamo. Actualiza la distancia entre el punto seleccionado y los naps.
  63. if (typeof calcularDistanciaNap === 'function') {
  64. calcularDistanciaNap();
  65. }
  66. // si existe la funcion la llamo. Actualiza direccion del cliente.
  67. if (typeof updateAddressClient === 'function') {
  68. updateAddressClient();
  69. }
  70. }
  71. function googleGeocoding(text, callResponse) {
  72. geocoder.geocode({address: text}, callResponse);
  73. }
  74. function formatJSON(rawjson) {
  75. var json = {},
  76. key;
  77. for (var i in rawjson) {
  78. key = rawjson[i].formatted_address;
  79. loc = new L.latLng(rawjson[i].geometry.location.lat(), rawjson[i].geometry.location.lng());
  80. json[key] = loc;
  81. }
  82. return json;
  83. }
  84. /**
  85. * Funcion que obtiene la direccion a partir de las coordenadas.
  86. * @param lat
  87. * @param lng
  88. * @param callback Funcion de callback el primer parametro contiene el status y el segundo un objeto json.
  89. */
  90. function geocodeLatLng(lat, lng, callback) {
  91. var latlng = {lat: lat, lng: lng};
  92. geocoder.geocode({'location': latlng}, function (results, status) {
  93. if (status == google.maps.GeocoderStatus.OK) {
  94. if (results[0]) {
  95. callback(true, {
  96. complete: results[0].formatted_address
  97. });
  98. } else {
  99. callback(false, status.status);
  100. }
  101. } else {
  102. callback(false, status.status);
  103. }
  104. });
  105. }