leaflet-map-widget.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. });
  34. $('.search-tooltip').on('click', function () {
  35. var tooltip = $(this).children('.search-tip');
  36. var location = '';
  37. if (tooltip) {
  38. location = tooltip.html();
  39. }
  40. if (location !== '') {
  41. $('.search-input').val(location);
  42. $('.search-tooltip').css('display', 'none');
  43. $('.search-button').trigger('click');
  44. }
  45. });
  46. });
  47. function setDataValue() {
  48. var jsonParseData = {};
  49. var dataValue = $('[name$="[extraData]"]').val();
  50. if (dataValue) {
  51. jsonParseData = JSON.parse(dataValue);
  52. }
  53. jsonParseData.lat = marker._latlng.lat;
  54. jsonParseData.lng = marker._latlng.lng;
  55. jsonParseData.zoom = map.getZoom();
  56. var stringifyData = JSON.stringify(jsonParseData);
  57. $('[name$="[extraData]"]').val(stringifyData);
  58. $('[name$="[map]"]').val(stringifyData);
  59. // si existe la funcion la llamo. Actualiza la distancia entre el punto seleccionado y los naps.
  60. if (typeof calcularDistanciaNap === 'function') {
  61. calcularDistanciaNap();
  62. }
  63. // si existe la funcion la llamo. Actualiza direccion del cliente.
  64. if (typeof updateAddressClient === 'function') {
  65. updateAddressClient();
  66. }
  67. }
  68. function googleGeocoding(text, callResponse) {
  69. geocoder.geocode({address: text}, callResponse);
  70. }
  71. function formatJSON(rawjson) {
  72. var json = {},
  73. key;
  74. for (var i in rawjson) {
  75. key = rawjson[i].formatted_address;
  76. loc = new L.latLng(rawjson[i].geometry.location.lat(), rawjson[i].geometry.location.lng());
  77. json[key] = loc;
  78. }
  79. return json;
  80. }
  81. /**
  82. * Funcion que obtiene la direccion a partir de las coordenadas.
  83. * @param lat
  84. * @param lng
  85. * @param callback Funcion de callback el primer parametro contiene el status y el segundo un objeto json.
  86. */
  87. function geocodeLatLng(lat, lng, callback) {
  88. var latlng = {lat: lat, lng: lng};
  89. geocoder.geocode({'location': latlng}, function (results, status) {
  90. if (status == google.maps.GeocoderStatus.OK) {
  91. if (results[0]) {
  92. callback(true, {
  93. complete: results[0].formatted_address
  94. });
  95. } else {
  96. callback(false, status.status);
  97. }
  98. } else {
  99. callback(false, status.status);
  100. }
  101. });
  102. }