123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- var geocoder;
- $(document).ready(function () {
- placeholder = 'Buscar...';
- geocoder = new google.maps.Geocoder();
- map.addControl(new L.Control.Search({
- sourceData: googleGeocoding,
- formatData: formatJSON,
- markerLocation: true,
- autoType: true,
- autoCollapse: false,
- minLength: 3,
- position: 'topright',
- delayType: 1,
- tooltipLimit: -1,
- firstTipSubmit: true,
- autoResize: false,
- collapsed: false,
- textPlaceholder: placeholder
- }));
- $('.search-button').on('click', function () {
- if (loc && $('.search-input').val() !== placeholder) {
- drawMarker();
- setDataValue();
- }
- });
- $('.search-input').on('keypress', function (e) {
- if (e.keyCode == 13 && loc) {
- e.preventDefault();
- e.stopPropagation();
- drawMarker();
- setDataValue();
- }
- if (typeof addAddressToExtraData === 'function') {
- addAddressToExtraData();
- }
- });
- $('.search-tooltip').on('click', function () {
- var tooltip = $(this).children('.search-tip');
- var location = '';
- if (tooltip) {
- location = tooltip.html();
- }
- if (location !== '') {
- $('.search-input').val(location);
- $('.search-tooltip').css('display', 'none');
- $('.search-button').trigger('click');
- }
- });
- });
- function setDataValue() {
- var jsonParseData = {};
- var dataValue = $('[name$="[extraData]"]').val();
- if (dataValue) {
- jsonParseData = JSON.parse(dataValue);
- }
- jsonParseData.lat = marker._latlng.lat;
- jsonParseData.lng = marker._latlng.lng;
- jsonParseData.zoom = map.getZoom();
- var stringifyData = JSON.stringify(jsonParseData);
- $('[name$="[extraData]"]').val(stringifyData);
- $('[name$="[map]"]').val(stringifyData);
- // si existe la funcion la llamo. Actualiza la distancia entre el punto seleccionado y los naps.
- if (typeof calcularDistanciaNap === 'function') {
- calcularDistanciaNap();
- }
- // si existe la funcion la llamo. Actualiza direccion del cliente.
- if (typeof updateAddressClient === 'function') {
- updateAddressClient();
- }
- }
- function googleGeocoding(text, callResponse) {
- geocoder.geocode({address: text}, callResponse);
- }
- function formatJSON(rawjson) {
- var json = {},
- key;
- for (var i in rawjson) {
- key = rawjson[i].formatted_address;
- loc = new L.latLng(rawjson[i].geometry.location.lat(), rawjson[i].geometry.location.lng());
- json[key] = loc;
- }
- return json;
- }
- /**
- * Funcion que obtiene la direccion a partir de las coordenadas.
- * @param lat
- * @param lng
- * @param callback Funcion de callback el primer parametro contiene el status y el segundo un objeto json.
- */
- function geocodeLatLng(lat, lng, callback) {
- var latlng = {lat: lat, lng: lng};
- geocoder.geocode({'location': latlng}, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- callback(true, {
- complete: results[0].formatted_address
- });
- } else {
- callback(false, status.status);
- }
- } else {
- callback(false, status.status);
- }
- });
- }
|