I have a creating marker function is JS that I want to call from C# :
function createMarker(position, information) {
var marker = new google.maps.Marker({
position: position,
map: map
});
google.maps.event.addListener(marker, "click", function () {
infowindow = new google.maps.InfoWindow({ content: information });
if (prev_infowindow) {
prev_infowindow.close();
}
prev_infowindow = infowindow;
infowindow.open(map, this);
});
google.maps.event.addListener(map, "click", function () { //supprime l'infobulle affichée au clic sur la map
infowindow.close();
});
}
At the beginning it only had 1 parameter and I was able to call it through c#, but now it has 2 parameters and I'm struggling to get it working. I always have a syntax error with ")" or ":".
foreach (XmlElement elementNode in parentNode)
{
if (elementNode.GetAttribute("latitude") != String.Empty && elementNode.GetAttribute("longitude") != String.Empty)
{
lat = double.Parse(elementNode.GetAttribute("latitude"), CultureInfo.InvariantCulture)/100000;
lng = double.Parse(elementNode.GetAttribute("longitude"), CultureInfo.InvariantCulture) / 100000;
latStr = ""+lat;
lngStr = ""+lng;
latStr = latStr.Replace(',', '.');
lngStr = lngStr.Replace(',', '.');
position = "{lat: " + latStr + ", lng:" + lngStr + "}";
if (DistanceTo(latitude, longitude, lat, lng) < distance)
{
cpt++;
infoStations = "Some random string" ;
jsFunc = "createMarker(" + position + "," + infoStations+ ")";
coordMarkers.Add(new List<double> { lat, lng });
ScriptManager.RegisterClientScriptBlock(this, GetType(), "script"+cpt, "$(function () {" + jsFunc+"; });", true);
}
}
}
Is it possible that the "," in the variable position is the problem ? Because if I only pass 1 parameter, it creates well the marker with an empty infoWindow, but with 2 parameters neither the markers or infowindow are showing.
Here is the working code :
jsFunc = "createMarker(" + position + ")";
ScriptManager.RegisterClientScriptBlock(this, GetType(), "script"+cpt, "$(function () {" + jsFunc+"; });", true);