I'm working on a web application for a disaster management lab assignment that is using the Google Places and Maps JavaScript API. The goal is to have markers on the map which are attached to an event listener which is supposed to show an information window with the data about a disaster report. However, the window is not showing up when I click on the marker. The pointer finger icon shows when I hover over a point, yet no information window appears when I click on the marker. There are zero errors in the dev console when I run it through IntelliJ and Tomcat, and I tried changing addListener to addEventListener but it still doesn't work. I will post my code below but let me know if you need anything else to help. For security reasons, I have replaced my API key with MY_API_KEY, so I guess you will have to have access to the Google API's yourself in order to help so I apologize for that. Thanks!
P.S.
When I tried creating the snippet it came up with the following error which I'm unsure where the error is coming from because there is no line 302 in the JS code:
{ "message": "Uncaught SyntaxError: Unexpected end of input", "filename": "https://stacksnippets.net/js", "lineno": 302, "colno": 5 }
Here's what the information window is supposed to look like:
//loadform.js
function onSelectReportType(ele) {
var form = $(ele).parent().parent();
var label = $(form).find(".additional_msg");
var select = $(form).find(".additional_msg_select");
switch (ele.value) {
case "donation":
case "request":
label.text("Resource Type:");
select.find('option').remove();
select.append($("<option></option>")
.attr("value", "")
.text("Choose the resource type"));
selectValues = ['water', 'food', 'money', 'medicine', 'cloth',
'rescue/volunteer'
];
$.each(selectValues, function(index, value) {
select.append($("<option></option>")
.attr("value", value)
.text(value));
});
break;
case "damage":
label.text("Damage Type:");
select.find('option').remove();
select.append($("<option></option>")
.attr("value", "")
.text("Choose the damage type"));
selectValues = ['polution', 'building damage', 'road damage', 'casualty',
'other'
];
$.each(selectValues, function(index, value) {
select.append($("<option></option>")
.attr("value", value)
.text(value));
});
break;
default:
$(form).find(".additional_msg_div").css("visibility", "hidden");
return;
}
$(form).find(".additional_msg_div").css("visibility", "visible");
//loadmap.js
var map;
var infowindow = new google.maps.InfoWindow();
function initialization() {
showAllReports();
}
function showAllReports() {
$.ajax({
url: 'HttpServlet',
type: 'POST',
data: {
"tab_id": "1"
},
success: function(reports) {
mapInitialization(reports);
},
error: function(xhr, status, error) {
alert("An AJAX error occured: " + status + "\nError: " + error);
}
});
}
function mapInitialization(reports) {
var mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP, // Set the type of Map
};
// Render the map within the empty div
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var bounds = new google.maps.LatLngBounds();
$.each(reports, function(i, e) {
var long = Number(e['longitude']);
var lat = Number(e['latitude']);
var latlng = new google.maps.LatLng(lat, long);
bounds.extend(latlng);
// Create the infoWindow content
var contentStr = '<h4>Report Details</h4><hr>';
contentStr += '<p><b>' + 'Disaster' + ':</b> ' + e['disaster'] + '</p>';
contentStr += '<p><b>' + 'Report Type' + ':</b> ' + e['report_type'] +
'</p>';
if (e['report_type'] == 'request' || e['report_type'] == 'donation') {
contentStr += '<p><b>' + 'Resource Type' + ':</b> ' +
e['resource_type'] + '</p>';
} else if (e['report_type'] == 'damage') {
contentStr += '<p><b>' + 'Damage Type' + ':</b> ' + e['damage_type'] +
'</p>';
}
contentStr += '<p><b>' + 'Timestamp' + ':</b> ' +
e['time_stamp'].substring(0, 19) + '</p>';
if ('message' in e) {
contentStr += '<p><b>' + 'Message' + ':</b> ' + e['message'] + '</p>';
}
// Create the marker
var marker = new google.maps.Marker({ // Set the marker
position: latlng, // Position marker to coordinates
map: map, // assign the market to our map variable
customInfo: contentStr,
});
// Add a Click Listener to the marker
google.maps.event.addEventListener(marker, 'click', function() {
// use 'customInfo' to customize infoWindow
infowindow.setContent(marker['customInfo']);
infowindow.open(map, marker); // Open InfoWindow
});
});
map.fitBounds(bounds);
}
//Execute our 'initialization' function once the page has loaded.
google.maps.event.addDomListener(window, 'load', initialization);
html,
body {
background-color: #f5f5f5;
}
body {
padding-top: 50px;
}
.container-fluid,
.row {
background-color: #f5f5f5;
}
.sidebar {
padding: 12px;
background-color: #f5f5f5;
}
form {
padding-top: 12px;
}
#map-canvas {
height: 100%;
position: absolute;
right: 0;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Web Project</title>
<!-- Custom styles -->
<link rel="stylesheet" href="css/style.css">
<!-- jQuery -->
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- Google Map js libraries -->
<script src="https://maps.googleapis.com/maps/api/js?&key=MY_API_KEY&libraries=places,visualization"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<a class="navbar-brand">Disaster Management Portal</a>
</nav>
<div class="container-fluid">
<div class="row">
<div class="sidebar col-xs-3">
<!-- Tab Navis-->
<ul class="nav nav-tabs">
<li class="active"><a href="#create_report" data-toggle="tab">Create Report</a></li>
<li><a href="#query_report" data-toggle="tab">Query</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content ">
<!-- Create Report Tab Panel -->
<div class="tab-pane active" id="create_report">
<form id="create_report_form">
<div><label>First Name: </label><input placeholder="Your first name" name="fN"></div>
<div><label>Last Name: </label><input placeholder="Your last name" name="lN"></div>
<div>
<label><input type="radio" name="is_male" value="t"> Male</label>
<label><input type="radio" name="is_male" value="f"> Female</label>
</div>
<div><label>Age: </label><input placeholder="Your age" name="age"></div>
<div><label>Blood Type:</label>
<select name="blood_type">
<option value="">Choose your blood type</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="O">O</option>
<option value="AB">AB</option>
<option value="Other">Other</option>
</select>
</div>
<div><label>Tel: </label><input placeholder="Your telephone number" name="tel"></div>
<div><label>Email: </label><input placeholder="Your email address" name="email"></div>
<div><label>Contact's First Name: </label><input placeholder="Contact's first name" name="contact_fN"></div>
<div><label>Contact's Last Name: </label><input placeholder="Contact's last name" name="contact_lN"></div>
<div><label>Contact's Tel: </label><input placeholder="Contact's telephone number" name="contact_tel"></div>
<div><label>Contact's Email: </label><input placeholder="Contact's email address" name="contact_email"></div>
<div><label>Report Type:</label>
<select onchange="onSelectReportType(this)" name="report_type">
<option value="">Choose the report type</option>
<option value="donation">Donation</option>
<option value="request">Request</option>
<option value="damage">Damage Report</option>
</select>
</div>
<div class="additional_msg_div" style="visibility: hidden"><label class="additional_msg"></label>
<select class="additional_msg_select" name="additional_message"></select>
</div>
<div><label>Disaster Type:</label>
<select name="disaster_type">
<option value="">Choose the disaster type</option>
<option value="flood">flood</option>
<option value="wildfire">wildfire</option>
<option value="earthquake">earthquake</option>
<option value="tornado">tornado</option>
<option value="hurricane">hurricane</option>
<option value="other">other</option>
</select>
</div>
<div><label>Address:</label>
<input id="autocomplete" placeholder="Address">
</div>
<div><label>Comment: </label><input placeholder="Additional message" name="message"></div>
<button type="submit" class="btn btn-default" id="report_submit_btn">
<span class="glyphicon glyphicon-star"></span> Submit
</button>
</form>
</div>
<!-- Query Report Tab Panel -->
<div class="tab-pane" id="query_report">
<form id="query_report_form">
<div><label>Report Type:</label>
<select onchange="onSelectReportType(this)" name="report_type">
<option value="">Choose the report type</option>
<option value="donation">Donation</option>
<option value="request">Request</option>
<option value="damage">Damage Report</option>
</select>
</div>
<div class="additional_msg_div" style="visibility: hidden"><label class="additional_msg"></label>
<select class="additional_msg_select" name="resource_or_damage"></select>
</div>
<div><label>Disaster Type:</label>
<select name="disaster_type">
<option value="">Choose the disaster type</option>
<option value="flood">flood</option>
<option value="wildfire">wildfire</option>
<option value="earthquake">earthquake</option>
<option value="tornado">tornado</option>
<option value="hurricane">hurricane</option>
<option value="other">other</option>
</select>
</div>
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-star"></span> Submit the query
</button>
</form>
</div>
</div>
</div>
<div id="map-canvas" class="col-xs-9"></div>
</div>
</div>
</body>
</html>
