(function() { this.DocMap = (function() { function DocMap(options) { if (options == null) { options = {}; } this.mapElement = document.getElementById('map'); if (!this.mapElement) { return; } this.waitForGoogleMapsAPI(function() { return this.initializeMap(options); }); } DocMap.prototype.waitForGoogleMapsAPI = function(callback) { if (typeof google === 'undefined' || ((typeof google !== "undefined" && google !== null ? google.maps : void 0) == null)) { return setTimeout(((function(_this) { return function() { return _this.waitForGoogleMapsAPI(callback); }; })(this)), 1000); } else { return callback.call(this); } }; DocMap.prototype.initializeMap = function(options) { if (options == null) { options = {}; } this.map = new google.maps.Map(this.mapElement, { zoom: options.zoom || 8, center: options.center || { lat: -34.397, lng: 150.644 } }); this.markers = []; this.setupAutocomplete(); this.setupGeolocation(); if (options.markersData != null) { return this.addMarkers(options.markersData); } }; DocMap.prototype.addMarkers = function(data) { var i, infowindow, lat, len, lng, marker, markerData; this.clearMarkers(); if (Array.isArray(data)) { for (i = 0, len = data.length; i < len; i++) { markerData = data[i]; lat = parseFloat(markerData.lat); lng = parseFloat(markerData.lng); if (isNaN(lat) || isNaN(lng)) { console.error("Invalid marker coordinates:", markerData); continue; } marker = new google.maps.Marker({ position: { lat: lat, lng: lng }, map: this.map, title: markerData.title }); if (markerData.infowindow) { infowindow = new google.maps.InfoWindow({ content: markerData.infowindow }); marker.addListener("click", function() { return infowindow.open(this.map, marker); }); } this.markers.push(marker); } return this.fitBoundsToMarkers(); } else { return console.error("Markers data is not an array:", data); } }; DocMap.prototype.clearMarkers = function() { var i, len, marker, ref; ref = this.markers; for (i = 0, len = ref.length; i < len; i++) { marker = ref[i]; marker.setMap(null); } return this.markers = []; }; DocMap.prototype.fitBoundsToMarkers = function() { var bounds, i, len, marker, ref; bounds = new google.maps.LatLngBounds(); ref = this.markers; for (i = 0, len = ref.length; i < len; i++) { marker = ref[i]; bounds.extend(marker.getPosition()); } return this.map.fitBounds(bounds); }; DocMap.prototype.setupAutocomplete = function() { var autocomplete, input, ref; input = document.getElementById('search_place'); if (!input) { return; } if (typeof google !== 'undefined' && ((typeof google !== "undefined" && google !== null ? (ref = google.maps) != null ? ref.places : void 0 : void 0) != null)) { autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'], componentRestrictions: { country: 'gr' } }); return google.maps.event.addListener(autocomplete, 'place_changed', (function(_this) { return function() { var place; place = autocomplete.getPlace(); if (!place.geometry) { return; } if (place.geometry.viewport != null) { _this.map.fitBounds(place.geometry.viewport); } if (place.geometry.viewport == null) { _this.map.setCenter(place.geometry.location); } $("#search_lat").val(place.geometry.location.lat()); return $("#search_lng").val(place.geometry.location.lng()); }; })(this)); } else { return setTimeout(((function(_this) { return function() { return _this.setupAutocomplete(); }; })(this)), 300); } }; DocMap.prototype.setupGeolocation = function() { return $('#find-me').click((function(_this) { return function() { if (navigator.geolocation) { return navigator.geolocation.getCurrentPosition(_this.handleGeolocation, _this.handleGeolocationError); } else { return alert("Geolocation is not supported by this browser."); } }; })(this)); }; DocMap.prototype.handleGeolocation = function(position) { var lat, lng, pos; lat = position.coords.latitude; lng = position.coords.longitude; pos = new google.maps.LatLng(lat, lng); this.map.setCenter(pos); $("#search_lat").val(lat); return $("#search_lng").val(lng); }; DocMap.prototype.handleGeolocationError = function(error) { return alert("Error occurred. Error code: " + error.code); }; return DocMap; })(); this.initializeMap = function() { var markersData; if ($('#map').length > 0) { markersData = JSON.parse($("#map").attr("data-markers") || '[]'); return new DocMap({ markersData: markersData }); } }; $(document).ready(function() { return initializeMap(); }); }).call(this);