I am having trouble with the markers' event listeners, here I have successfully populated the map using LINQ in server-side
var x = from a in db.project_profiles
select new
{
a.project_gis_latitude,
a.project_gis_longitude,
a.project_title
};
Csm
string csName = "MapScript";
Type csType = this.GetType();
ClientScriptManager csm = Page.ClientScript;
if (!csm.IsStartupScriptRegistered(csType, csName))
{
StringBuilder sb = new StringBuilder();
and this is the entire string for the map
sb.Append(" <script> ");
sb.Append(" var myLatLng = {lat: 8.2784189, lng: 125.5922004}; ");
//sb.Append("alert(myLatLng.lat);");
sb.Append(" var markers = [];");
sb.Append(" function initMap() { ");
sb.Append(" var map = new google.maps.Map(document.getElementById('dvMap'), { ");
sb.Append(" zoom: 7, ");
sb.Append(" center: myLatLng ");
sb.Append(" }); ");
foreach (var z in x)
{
//sb.Append("alert('"+z.project_title+"');");
sb.Append(" var lat_lng = new google.maps.LatLng(" + z.project_gis_latitude + ", "+z.project_gis_longitude+"); ");
sb.Append(" var marker = new google.maps.Marker({ ");
sb.Append(" position: lat_lng, ");
sb.Append(" map: map, ");
sb.Append(" title: '"+z.project_title+"', ");
sb.Append(" label: '" + z.project_title.Substring(0,1) + "' ");
sb.Append(" }); ");
sb.Append(" marker.addListener('click', function() { ");
sb.Append(" map.setZoom(10); ");
sb.Append(" map.setCenter(marker.getPosition()); ");
sb.Append(" }); ");
};
//sb.Append("alert('End');");
sb.Append(" } ");
sb.Append("initMap();");
sb.Append(" </script> ");
and this is the end of the script
csm.RegisterStartupScript(csType, csName, sb.ToString());
there was no error when it runs and all the markers appeared but the listener was only responding to the last marker loaded, I want all the markers to have the listener not just one so I can retrieve their titles when clicked.
i tried to move the listener block out of the foreach group but the same thing happened and only the last marker can be clicked.
and this is the html for the map
<div id="dvMap"></div>