I am trying to check and see if an image exists so I can adjust my contentString accordingly. However, when I try to do this, only one pin shows up on the map (the first one).
var checkins = <?php echo $json; ?>;
function imageExists(url, callback) {
var img = new Image();
img.onload = function() { callback(true); };
img.onerror = function() { callback(false); };
img.src = url;
}
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: 39.8282, lng: -98.5795}
});
setMarkers(map);
}
function setMarkers(map) {
var infowindow = new google.maps.InfoWindow({
content: ""
});
for (var i = 0; i < checkins.length; i++) {
var checkin = checkins[i];
var marker = new google.maps.Marker({
position: {lat: parseFloat(checkin[2]), lng: parseFloat(checkin[3])},
map: map,
title: checkin[1]
});
var imageUrl = 'http://rjbogz.ddns.net/pictures/'+checkin[0]+'.jpg';
imageExists(imageUrl, function(exists) {
if (exists){
contentString = '<b>'+checkin[6]+'</b><br><b>'+checkin[4]+'</b> by <b>'+checkin[5]+'</b><br>At <b>'+checkin[1]+'</b><br><img src="pictures/'+checkin[0]+'.jpg" style="width:300px;height:300px;">';
bindInfoWindow(marker, map, infowindow, contentString);
} else {
contentString = '<b>'+checkin[6]+'</b><br><b>'+checkin[4]+'</b> by <b>'+checkin[5]+'</b><br>At <b>'+checkin[1]+'</b>';
bindInfoWindow(marker, map, infowindow, contentString);
}
});
}
}
function bindInfoWindow(marker, map, infowindow, description) {
marker.addListener('click', function() {
infowindow.setContent(description);
infowindow.open(map, this);
});
}
As you can see by the imageExists
function, I tried adding a check to see if the image exists, but then it only plots one pin on the map. The website is currently published to http://rjbogz.ddns.net/map.php if you want to take a look at what it looks like. An example of what I want to fix would be if you click on the icon in Bermuda (there is no image file associated with it).