I am trying to plot multiple markers on a google map and need some help. I am taking the locations' longitude and latitude from json data being returned from an ajax api call.
I am following along the following post to implement this feature, but it's not working and I'm not sure why. Google Maps JS API v3 - Simple Multiple Marker Example
I think one of my issues are that my locations variable is not being populated with an array so when I do .length its returning to me a number like 23 even though there are only 3 locations in the result set.
var locations = [];
var map;
function initMap() {
map = new google.maps.Map(document.querySelector('.map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 8
});
}
$.ajax({
url: api/com...
success: function (result) {
for (const row of result.payload) {
locations = 'lat: ' + row.latitude + ', ' + 'lng: ' +
row.longitude;
}
for (i = 0; i < locations.length; i++) {
console.log(locations.length);
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
data: {
name: locations[i][0]
}
});
marker.addListener('click', function() {
if(!this.infoWindow) {
this.infoWindow = new google.maps.InfoWindow({
content: this.data.name
});
}
this.infoWindow.open(map,this);
})
};
}
});