var infoWindow = false; var exportIds = []; var markerMap = {}; function initialiseLocations() { if(!infoWindow) { infoWindow = new google.maps.InfoWindow(); } $.get("/api/customers/" + customerId + "/get-locations", { id: $("#id").val(), internal_id: $("#internal-id").val(), name: $("#name").val() }, function(response) { if(!response.success) return false; if(!response.locations) return false; let map = maps["map"]; let bounds = new google.maps.LatLngBounds; for(let i in response.locations) { let location = response.locations[i]; let position = { lat: location.latitude, lng: location.longitude }; let options = { map: map, position: position, title: location.name } if(location.icon) { options.icon = "/img/markers/building-red.png"; } bounds.extend(position); let key = "marker-" + location.id; let marker = new google.maps.Marker(options); marker.location = location; marker.addListener("click", function() { displayInfoWindow(this); }); markerMap[key] = marker; } map.fitBounds(bounds); }); $(document).on("click", "a[data-export-location-id]", function(event) { let locationId = parseInt($(this).attr("data-export-location-id")); let index = exportIds.indexOf(locationId); let key = "marker-" + locationId; let marker = markerMap[key]; if(index == -1) { exportIds.push(locationId); marker.setOptions({ icon: "/img/markers/building-green.png" }); } else { exportIds.splice(index, 1); marker.setOptions({ icon: "/img/markers/building-red.png" }); } displayInfoWindow(marker); }); $("#export-csv-button, #export-kml-button").click(function(event) { if(exportIds.length > 0) { event.preventDefault(); let action = $(this).attr("data-action"); $("#locations-export-form input[name='ids[]']").remove(); $("#locations-export-form input[name='action']").val(action); for(let i in exportIds) { let locationId = exportIds[i]; $("#locations-export-form").append(""); } $("#locations-export-form").submit(); } }); } function displayInfoWindow(marker) { let locationId = parseInt(marker.location.id); let index = exportIds.indexOf(locationId); let exportText = "Select for Export"; let exportClass = "btn-primary"; if(index > -1) { exportText = "Deselect for Export"; exportClass = "btn-success"; } let content = "
"; content += "

" + marker.location.name + "

\n"; content += "
"; content += ""; content += ""; content += "
"; content += "
"; infoWindow.setContent(content); infoWindow.open(map, marker); }