Reputation: 325
In brief, I want to combine these two topics:
How to add an image to InfoWindow of marker in google maps v3?
Google Maps JS API v3 - Simple Multiple Marker Example
I have my location data stored in a database and it is retrieved as an array. I want to place a marker and infowindow for each of the x amount of locations in the database. One of fields contains an Image and I want to include this in the infoWindow content.
Here is where I am so far:
Php: // Create connection $con=mysqli_connect("*******","*******","*******","tests");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Name, Latitude, Longitude, Image FROM locate_coords");
while($row = mysqli_fetch_array($result))
{
$users[]= $row;
}
Javascript function initialize()
:
<?php
$js_array = json_encode($users);
echo "var locations = ". $js_array . ";\n";
?>
var styles = [
{
stylers: [
{ hue: "#00ffe6" },
{ saturation: -20 },
]
},{
featureType: "road",
elementType: "geometry",
stylers: [
{ lightness: 100 },
{ visibility: "simplified" },
]
},{
featureType: "road",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
var styledMap = new google.maps.StyledMapType(styles,
{name: "Styled Map"});
var mapProp = {
zoom: 12,
center: new google.maps.LatLng(51.508742,-0.120850),
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapProp);
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
//var container = document.getElementById("photo");
//var img = new Image();
//img.src = '
//container.appendChild(img);
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
//infowindow.setContent('<img id=="photo">' + locations[i][0]);
infowindow.setContent('<img src=/uploads/'. locations[i][3]'>' + locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');
}
google.maps.event.addDomListener(window, 'load', initialize);
Putting a normal image url in works nicely, but as soon as I try putting an array value, the map doesn't want to load! At this point, I really could do with a sane head to tell me what to do :-(..
Upvotes: 3
Views: 1772
Reputation: 1567
There is a typo in the line
infowindow.setContent('<img src=/uploads/'. locations[i][3]'>' + locations[i][0]);
. is not the concatenation operator and between locations[i][3]'>'
there is no concatenation operator at all. The correct code would be:
infowindow.setContent('<img src=/uploads/' + locations[i][3] + '>' + locations[i][0]);
Upvotes: 4