I have a map set up with Google maps v3 where markers are saved to a DB. A new markers info window contains a form which saves data to a PHP database. When the map is loaded, the markers are displayed and corresponding info can be seen when they are clicked. The problem here is that all the markers display the information that is saved with the last row inserted.
This loads data from the DB:
// Get markers from XML - (event_data.php)
$.get("event_data.php", function (data) {
$(data).find("markers").each(function () {
var name = $(this).attr('name');
var description = '<p>'+ $(this).attr('description') +'</p>';
var category = $(this).attr('category');
var edate = $(this).attr('edate');
var point = new google.maps.LatLng(parseFloat($(this).attr('lat')),parseFloat($(this).attr('lon')));
create_marker(point, name, description, category, edate, false, false, false, "static/assets/new_event_marker.png");
});
});
And This is what displays the saved markers:
function create_marker(MapPos, eName, eDesc, eCateg, eDate, InfoOpenDefault, DragAble, Removable, iconPath)
{
var marker = new google.maps.Marker({
position: MapPos,
map: map,
draggable:DragAble,
title: eName,
icon: iconPath
});
// Content to be displayed in event InfoWindows
var eventContent = $('<div class="event-details">' + '<h4 class="event-name">' + eName + '</h4><hr>' +
'<span><h5>Date: </h5>' +
'<p class="event-date">' + eDate + '</p></span>' +
'<p class="event-description">'+ eDesc +'</p>' +
'<button type="button" name="remove-event" class="remove-event btn btn-warning btn-sm"><span class="glyphicon glyphicon-remove"></span> Remove Event</button>'+
'</div>');
//set the content of infoWindow
infowindow.setContent(eventContent[0]);
When I run event_data.php in the browser, it returns all the event markers that have been saved.
I also have run console.log(eName); right before "var marker", and it returns all the items in the database, but with 1 extra with"undefined":
undefined
event1
event2
event3
fiddle demonstrating issue
See Question&Answers more detail:
os