271 lines
10 KiB
JavaScript
271 lines
10 KiB
JavaScript
// инициализирует виджет DaData для поля Адрес
|
|
// Docs: https://confluence.hflabs.ru/pages/viewpage.action?pageId=207454322
|
|
function daDataInitComplex(constraintsList, iso = "RU") {
|
|
//console.log(iso);
|
|
let TOKEN = "4cd95e0f20b5f5e0adb1454d277e2f9014c2ddcb";//"2f6595e671df25163a441a544bbed3683791c5d0";
|
|
if (constraintsList.length > 0) {
|
|
//console.log(kladr_ids);
|
|
$("#address_complex").suggestions({
|
|
token: TOKEN,
|
|
type: "ADDRESS",
|
|
|
|
// вызывается, когда пользователь выбирает одну из подсказок
|
|
onSelect: onSelectSuggestionComplex,
|
|
constraints: constraintsList
|
|
});
|
|
} else {
|
|
$("#address_complex").suggestions({
|
|
token: TOKEN,
|
|
type: "ADDRESS",
|
|
constraints: {
|
|
locations: [
|
|
{ country_iso_code: iso },
|
|
|
|
]
|
|
},
|
|
// вызывается, когда пользователь выбирает одну из подсказок
|
|
onSelect: onSelectSuggestionComplex
|
|
});
|
|
}
|
|
}
|
|
|
|
// обработчик, когда была выбрана одно из подсказок виджета DaData
|
|
function onSelectSuggestionComplex(suggestion) {
|
|
$('.suggestForm').show();
|
|
$('.adres2 .more-block input').val('');
|
|
|
|
if (suggestion.data.qc_geo !== "0") {
|
|
//если выбрана точность до дома, то пробуем получить координаты из яндекса
|
|
if (suggestion.data.fias_level === "8" || suggestion.data.fias_level === "7" || suggestion.data.fias_level === "65") {
|
|
$.ajax({
|
|
url: '/ajax/getCoordinatesFromYandex.php',
|
|
method: "post",
|
|
data: {
|
|
"address": $("#address_complex").val()
|
|
},
|
|
success: function(data) {
|
|
let found = false;
|
|
if (data && data.indexOf('success:') === 0) {
|
|
data = data.replace('success:', '');
|
|
let coords = data.split(' ');
|
|
if (coords[0] && coords[1]) {
|
|
found = true;
|
|
|
|
myMapComplex.setCenter([coords[1], coords[0]], 14);
|
|
addPlacemarkComplex(coords[1], coords[0]);
|
|
$('.help-block.has-error[data-for="#address_complex"]').remove();
|
|
}
|
|
}
|
|
if (!found) {
|
|
if (!$('.help-block.has-error[data-for="#address_complex"]').length)
|
|
$('input#address_complex').after('<div class="help-block has-error" data-for="#address_complex" style="color:red;font-size:12px;">Необходимо уточнить координаты объекта на карте</div>');
|
|
}
|
|
},
|
|
error: function(XMLHttpRequest, textStatus, errorThrown) {
|
|
console.log("eRROR: " + errorThrown + "; status:" + textStatus);
|
|
}
|
|
});
|
|
} else {
|
|
if (!$('.help-block.has-error[data-for="#address_complex"]').length)
|
|
$('input#address_complex').after('<div class="help-block has-error" data-for="#address_complex" style="color:red;font-size:12px;">Необходимо уточнить координаты объекта на карте</div>');
|
|
}
|
|
} else {
|
|
$('.help-block.has-error[data-for="#address_complex"]').remove();
|
|
}
|
|
onYandexMapInitComplex
|
|
|
|
complexform.processAddress(suggestion.data);
|
|
|
|
$('#map-complex').show();
|
|
|
|
myMapComplex.setCenter([suggestion.data.geo_lat, suggestion.data.geo_lon], 14);
|
|
addPlacemarkComplex(suggestion.data.geo_lat, suggestion.data.geo_lon);
|
|
}
|
|
|
|
function suggestAddress(address) {
|
|
if (typeof address == "string") {
|
|
let query = {
|
|
query: address,
|
|
count: 1
|
|
};
|
|
let url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address";
|
|
let token = "4cd95e0f20b5f5e0adb1454d277e2f9014c2ddcb";//"2f6595e671df25163a441a544bbed3683791c5d0";
|
|
let options = {
|
|
method: "POST",
|
|
//mode: "cors",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
"Authorization": "Token " + token
|
|
},
|
|
body: JSON.stringify(query)
|
|
};
|
|
fetch(url, options)
|
|
.then((response) => {
|
|
return response.json();
|
|
})
|
|
.then((result) => {
|
|
let data = result.suggestions[0].data;
|
|
processAddress(data)
|
|
})
|
|
.catch(error => console.log("error", error));
|
|
}
|
|
}
|
|
|
|
function getAddressByCoords(cords, input) {
|
|
let url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/geolocate/address";
|
|
let token = "4cd95e0f20b5f5e0adb1454d277e2f9014c2ddcb";//"2f6595e671df25163a441a544bbed3683791c5d0";
|
|
let options = {
|
|
method: "POST",
|
|
mode: "cors",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json",
|
|
"Authorization": "Token " + token
|
|
},
|
|
body: JSON.stringify(cords)
|
|
};
|
|
|
|
fetch(url, options)
|
|
.then((response) => {
|
|
return response.json();
|
|
})
|
|
.then((result) => {
|
|
if (result.suggestions[0]) {
|
|
let data = result.suggestions[0].data;
|
|
let address = result.suggestions[0].value;
|
|
|
|
if ($('.help-block.has-error[data-for="#address_complex"]').length)
|
|
$('.help-block.has-error[data-for="#address_complex"]').remove();
|
|
|
|
if (typeof address !== "undefined" && data.qc_geo == "0") {
|
|
$(input).val(address);
|
|
//processAddress(data);
|
|
suggestAddress(address);
|
|
}
|
|
}
|
|
})
|
|
.catch(error => console.log("error", error));
|
|
}
|
|
|
|
var addPlacemarkComplex, setCenterComplex, myMapComplex, suggestComplex;
|
|
|
|
function onYandexMapInitComplex() {
|
|
|
|
let geo_lat = '62.090416';
|
|
if (typeof geo_lat !== "undefined") {
|
|
geo_lat.replace(/[^\.\d]/g, '');
|
|
}
|
|
|
|
let geo_lon = '129.74362';
|
|
if (typeof geo_lon !== "undefined") {
|
|
geo_lon.replace(/[^\.\d]/g, '');
|
|
}
|
|
|
|
var placemark = null;
|
|
myMapComplex = new yandexMapComplex.Map('map-complex', {
|
|
center: [
|
|
geo_lat,
|
|
geo_lon
|
|
],
|
|
zoom: 3,
|
|
controls: ['fullscreenControl', 'zoomControl']
|
|
}),
|
|
objectManager = new yandexMapComplex.ObjectManager({
|
|
clusterize: true,
|
|
clusterDisableClickZoom: true,
|
|
clusterOpenBalloonOnClick: false,
|
|
geoObjectOpenBalloonOnClick: false
|
|
});
|
|
|
|
objectManager.objects.options.set('iconColor', '#43a047');
|
|
|
|
addPlacemarkComplex = function(latitude, longitude) {
|
|
|
|
if (!latitude || !longitude) {
|
|
return;
|
|
}
|
|
|
|
if (typeof latitude !== "undefined")
|
|
latitude.toString().replace(/[^\.\d]/g, '');
|
|
|
|
if (typeof longitude !== "undefined")
|
|
longitude.toString().replace(/[^\.\d]/g, '');
|
|
|
|
if (placemark == null) {
|
|
placemark = new yandexMapComplex.Placemark([
|
|
Number.parseFloat(latitude).toPrecision(9),
|
|
Number.parseFloat(longitude).toPrecision(9)
|
|
], {}, { draggable: true });
|
|
myMapComplex.geoObjects.add(placemark);
|
|
|
|
/* Событие dragend - получение нового адреса */
|
|
placemark.events.add('dragend', function(e) {
|
|
let data = e.get('target').geometry.getCoordinates();
|
|
let cords = {
|
|
lat: data[0].toPrecision(9),
|
|
lon: data[1].toPrecision(9),
|
|
}
|
|
$('.latitude').val(cords.lat);
|
|
$('.longitude').val(cords.lon);
|
|
});
|
|
} else {
|
|
placemark.geometry.setCoordinates([Number.parseFloat(latitude).toPrecision(9), Number.parseFloat(longitude).toPrecision(9)]);
|
|
}
|
|
},
|
|
|
|
setCenterComplex = function() {
|
|
|
|
yandexMapComplex.geocode(complexform.region_label.replace(/\s+/g, ' ').trim()).then(function(res) {
|
|
var coord = res.geoObjects.get(0).geometry.getCoordinates();
|
|
|
|
myMapComplex.setCenter(coord, 6);
|
|
addPlacemarkComplex(coord[0].toPrecision(6), coord[1].toPrecision(6));
|
|
|
|
});
|
|
}
|
|
|
|
|
|
if (!isEmpty(complexform.complex.latitude) || !isEmpty(complexform.complex.latitude)) {
|
|
myMapComplex.setCenter([
|
|
(!isEmpty(complexform.complex.latitude) ? complexform.complex.latitude : "62.090416"),
|
|
(!isEmpty(complexform.complex.longitude) ? complexform.complex.longitude : "129.74362")
|
|
], 6);
|
|
addPlacemarkComplex((!isEmpty(complexform.complex.latitude) ? complexform.complex.latitude : "62.090416"),
|
|
(!isEmpty(complexform.complex.longitude) ? complexform.complex.longitude : "129.74362"));
|
|
} else {
|
|
if (!isEmpty(complexform.region_label)) {
|
|
setCenterComplex();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
$('body').on('click', '#map-modal-complex .closer', function() {
|
|
$('#map-modal-complex').hide();
|
|
$('#after_map-complex').hide();
|
|
return false;
|
|
})
|
|
/*var links = $('link');
|
|
if (!isEmpty(links)) {
|
|
for (var i = 0; i < links.length; i++) {
|
|
if ($(links[i]).attr('href') == 'https://cdn.jsdelivr.net/npm/suggestions-jquery@20.3.0/dist/css/suggestions.min.css') {
|
|
console.log('asdad');
|
|
}
|
|
}
|
|
}
|
|
|
|
var scripts = $('script');
|
|
if (!isEmpty(scripts)) {
|
|
for (var i = 0; i < scripts.length; i++) {
|
|
if ($(scripts[i]).attr('src') == 'https://api-maps.yandex.ru/2.1/?apikey=692f0a05-cb88-4158-88b9-06bc0dc93004&load=package.standard&lang=ru-RU&ns=yandexMap&onload=onYandexMapInit') {
|
|
console.log('asdadsrc');
|
|
}
|
|
}
|
|
}*/
|
|
|
|
|
|
|
|
}) |