Back to Search
} // get closest lattitude and longtude from array function rad(x) { return x * Math.PI / 180; } function getClosestCoordinates(latitude, longitude) { var lat = latitude; var lng = longitude; var R = 6371; // radius of earth in km var distances = []; var closest = -1; for (i = 0; i < jsonData.length; i++) { var mlat = jsonData[i].latitude; var mlng = jsonData[i].longitude; var dLat = rad(mlat - lat); var dLong = rad(mlng - lng); var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong / 2) * Math.sin(dLong / 2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); var d = R * c; distances[i] = d; jsonData[i].distance = d; if (closest == -1 || d < distances[closest]) { closest = i; } } jsonData.sort((a,b) => a.distance - b.distance ); return jsonData[0]; } function printMap() { let section = $('.s-find-form-result') let results = section.find('.s-find-form-result__item') let resultsContainer = section.find('.s-find-form-result__block-list') let bounds = new google.maps.LatLngBounds(); map = new google.maps.Map(document.getElementById("map"), { styles: [ { featureType: "administrative", elementType: "labels", stylers: [ { visibility: "on", }, ], }, { featureType: "administrative.country", elementType: "geometry.stroke", stylers: [ { visibility: "on", }, ], }, { featureType: "administrative.province", elementType: "geometry.stroke", stylers: [ { visibility: "on", }, ], }, { featureType: "landscape", elementType: "geometry", stylers: [ { visibility: "on", }, { color: "#F5F5F5", }, ], }, { featureType: "landscape.natural", elementType: "labels", stylers: [ { visibility: "off", }, ], }, { featureType: "poi", elementType: "all", stylers: [ { visibility: "off", }, ], }, { featureType: "road", elementType: "all", stylers: [ { color: "#C9C9C9", }, ], }, { featureType: "road", elementType: "labels", stylers: [ { visibility: "off", }, ], }, { featureType: "transit", elementType: "labels.icon", stylers: [ { visibility: "off", }, ], }, { featureType: "transit.line", elementType: "geometry", stylers: [ { visibility: "off", }, ], }, { featureType: "transit.line", elementType: "labels.text", stylers: [ { visibility: "on", }, ], }, { featureType: "transit.station.airport", elementType: "geometry", stylers: [ { visibility: "off", }, ], }, { featureType: "transit.station.airport", elementType: "labels", stylers: [ { visibility: "off", }, ], }, { featureType: "water", elementType: "geometry", stylers: [ { color: "#C9C9C9", }, ], }, { featureType: "water", elementType: "labels", stylers: [ { visibility: "off", }, ], }, ], }); let markerImage = { url: TEMPLATE_PATH + 'images/map-pin-icon.svg', scaledSize: new google.maps.Size(26, 37), origin: new google.maps.Point(0, 0) } results.each(function () { let result = $(this) let lng = result.attr('data-lng') let lat = result.attr('data-lat') let position = new google.maps.LatLng(lat, lng); let marker = new google.maps.Marker({ position: position, icon: markerImage, map: map, }); // checkMarkerVisible(marker); if (!city) { bounds.extend(marker.position); } google.maps.event.addListener(marker, 'click', function () { if ($(window).width() > windowMobile) { console.log("scroll",$(result).offset().top); //let itemsBefore = result.prevAll().length if(!$(result).hasClass('active')) { $(result).addClass('active'); $(result).siblings().removeClass('active'); resultsContainer.animate({scrollTop: 0}, {duration: 0}); setTimeout(() => { resultsContainer.animate({scrollTop: $(result).position().top + "px"}, {duration: 300}); }, 300); } } else { $('html,body').animate({ scrollTop: (result.position().top - $('.header').height() - 23) + "px" }, { duration: 1E3 }); } }); }) if (city) { map.setZoom(12); map.setCenter(new google.maps.LatLng(closest.latitude, closest.longitude)); } else { map.fitBounds(bounds); } //google.maps.event.removeListener(listener); if ($(window).width() <= windowMobile) return; //listener = google.maps.event.addListener(map, "idle", function () { //}); } //jQuery(document).ready(function () { // append code //})