I have been trying to figure this out now for a while, but cant seem to get it done! I'm getting data from a database in Laravel via Ajax and then are trying to get the markers to show infowindows for every marker. The markers place themself on the different addresses where I want them but they all share the same infowindow. (which is information from the last row in the database).
I tried to implement this solution in my code: Google Maps API v3 - Markers All Share The Same InfoWindow
But it didnt work...
My code looks like this:
var app = new Vue({
el: 'body',
data: {
users: $.getJSON("http://localhost:8000/data", function(data){
var map = new google.maps.Map(document.querySelector('#map'), {
center: {lat: 57.708870, lng: 11.974560 },
zoom: 14
});
var geocoder = new google.maps.Geocoder();
function bindInfoWindow(marker, map, infowindow, html) {
marker.addListener('click', function() {
infowindow.setContent(html);
infowindow.open(map, this);
});
}
for (var i = 0; i < data.length; i++) {
var address = data[i]['address'] + ' Göteborg';
var contentString = '<h4 style="color: #ffc62d">' + data[i]['foodtruck_name'] + '</h4>'
+ '<b>Mat:</b> ' + data[i]['type_of_food']
+ '<br><b>Öppettider:</b> '+ data[i]['open_hours']
+ '<br><b>Adress:</b> '+ data[i]['address']
+ '<br><b>Hemsida:</b> '+ '<a href="http://' + data[i]['webadress'] + '" target="_blank">' + data[i]['webadress'] + '</a>';
var image = {
url: 'http://localhost:8000/img/foodtruck.png',
// This marker is 20 pixels wide by 32 pixels high.
size: new google.maps.Size(45, 30),
// The origin for this image is (0, 0).
origin: new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at (0, 32).
anchor: new google.maps.Point(0, 30)
};
var shape = {
coords: [1, 1, 1, 30, 45, 20, 18, 1],
type: 'poly'
};
var infoWindow = new google.maps.InfoWindow({
maxWidth: 250
});
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: image,
shape: shape
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
bindInfoWindow(marker, map, infoWindow, contentString);
});
};
})
},
methods: {
createMap: function() {
var map = new google.maps.Map(document.querySelector('#map'), {
center: {lat: 57.708870, lng: 11.974560 },
zoom: 14
});
}
}
});
Anyone have an idea or an example on how to fix this? It's driving me nuts! :-(
This is the json that is returned from the data-object:
[{"foodtruck_name":"Emils Foodtruck","open_hours":"11:00-16:00","address":"Stigbergsliden 9","type_of_food":"Mexikanskt tema","webadress":"www.emilwallgren.se"},{"foodtruck_name":"Kodameras Truck","open_hours":"08:00-17:00","address":"F\u00f6rsta L\u00e5nggatan 16","type_of_food":"Cookies","webadress":"www.kodamera.se"}]