var markerGroups = []; var infoWindow = false; $(function() { $("a[data-customer-id]").click(function(event) { let customerId = $(this).data("customer-id"); let key = "customer-" + customerId; if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { let m = markerGroups[key][i]; m.setMap(null); } } let selected = $(this).data("selected"); if(selected) { $(this).data("selected", false); $(this).find(".badge").removeClass("badge-success").text(""); } else { loadCustomerLocations(customerId); } return false; }); $("a[data-project-id]").click(function(event) { let projectId = $(this).data("project-id"); let key = "project-" + projectId; if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { let m = markerGroups[key][i]; m.setMap(null); } } let selected = $(this).data("selected"); if(selected) { $(this).data("selected", false); $(this).find(".badge").removeClass("badge-success").text(""); } else { loadProjectLocations(projectId); } return false; }); $("a[data-all-addresses]").click(function(event) { let key = "all-addresses"; if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { let m = markerGroups[key][i]; m.setMap(null); } } let selected = $(this).data("selected"); if(selected) { $(this).data("selected", false); $(this).find(".badge").removeClass("badge-success").text(""); } else { loadAllAddresses(); } return false; }); }); function loadCustomerLocations(customerId) { if(infoWindow == false) { infoWindow = new google.maps.InfoWindow(); } $.get("/account/map-overview/get-customer-locations", { customer_id: customerId, submit: $("#submit").val() }, function(response) { if(!response.success) return false; let map = maps["map"]; let key = "customer-" + customerId; markerGroups[key] = []; for(let i in response.locations) { let l = response.locations[i]; let position = { lat: l.latitude, lng: l.longitude }; var options = { position: position, title: l.name, map: map }; if(l.icon) options.icon = "/img/markers/" + l.icon; infoWindow.setContent(""); let marker = new google.maps.Marker(options); addMarkerClickListenerForLocation(marker, l); markerGroups[key].push(marker); } $("a[data-customer-id=" + customerId + "]").data("selected", true); $("a[data-customer-id=" + customerId + "]").find(".badge").addClass("badge-danger").text(response.locations.length); }); } function loadProjectLocations(projectId) { if(infoWindow == false) { infoWindow = new google.maps.InfoWindow(); } $.get("/account/map-overview/get-project-locations", { project_id: projectId, submit: $("#submit").val() }, function(response) { if(!response.success) return false; let map = maps["map"]; let key = "project-" + projectId; markerGroups[key] = []; for(let i in response.locations) { let l = response.locations[i]; let position = { lat: l.latitude, lng: l.longitude }; var options = { position: position, title: l.name, map: map }; if(l.icon) options.icon = "/img/markers/" + l.icon; let marker = new google.maps.Marker(options); addMarkerClickListenerForLocation(marker, l); markerGroups[key].push(marker); } $("a[data-project-id=" + projectId + "]").data("selected", true); $("a[data-project-id=" + projectId + "]").find(".badge").addClass("badge-danger").text(response.locations.length); }); } function loadAllAddresses() { if(infoWindow == false) { infoWindow = new google.maps.InfoWindow(); } $.get("/api/addresses/get-all", { submit: $("#submit").val() }, function(response) { if(!response.success) return false; let map = maps["map"]; let key = "all-addresses"; markerGroups[key] = []; for(let i in response.addresses) { let a = response.addresses[i]; let position = { lat: a.latitude, lng: a.longitude }; var options = { position: position, title: a.name, map: map }; let marker = new google.maps.Marker(options); addMarkerClickListenerForAddress(marker, a); markerGroups[key].push(marker); } $("a[data-all-addresses]").data("selected", true); $("a[data-all-addresses]").find(".badge").addClass("badge-danger").text(response.addresses.length); }); } function addMarkerClickListenerForLocation(m, l) { let map = maps["map"]; m.addListener("click", function() { let content = "" + l.name + "
"; if(l.internalId) { content += "Internal ID: " + l.internalId + "
"; } content += "ID: " + l.id + "
"; infoWindow.setContent(content); infoWindow.open(map, m); }); } function addMarkerClickListenerForAddress(m, a) { let map = maps["map"]; m.addListener("click", function() { let content = "" + a.name + "
"; content += "ID: " + a.userId + "
"; infoWindow.setContent(content); infoWindow.open(map, m); }); }