Reputation: 2407
I have the following javascript function in the head of a view
function addMarker(title, description, lon,lat, iconT) {
var icon = new OpenLayers.Icon(iconT);
var markerslayer = new OpenLayers.Layer.Markers("Markers");
var lonlat = new OpenLayers.LonLat(lon, lat);
lonlat = lonlat.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
var markerToAdd = new OpenLayers.Marker(lonlat, icon);
markerToAdd.icon.imageDiv.title = title + " - " + description;
markerslayer.addMarker(markerToAdd);
map.addLayer(markerslayer);
map.addControl(new OpenLayers.Control.LayerSwitcher());
}
I am trying to call the function above by using either:
@if (Model != null)
{
foreach (var item in Model)
{
<script type="text/javascript">
addMarker(@item.Title, @item.Description, @item.Longitude, @item.Latiude, @item.Icon);
</script>
}
}
or alternatively
public ActionResult Index()
{
List<CommonLayer.Map> userMaps = new BL.Map().getUserMaps(User.Identity.Name);
foreach (var item in userMaps)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction()", true);
}
return View(userMaps);
}
What am I doing wrong? The view code is not calling the addMarket function whilst the c# code cannot be called from inside the foreach
since it is not static
Upvotes: 2
Views: 649
Reputation: 156978
Try this:
@if (Model != null)
{
foreach (var item in Model)
{
<script type="text/javascript">
addMarker('@item.Title', '@item.Description', @item.Longitude, @item.Latiude, @item.Icon);
</script>
}
}
Also, this line has the icon
misspelled (must be iconT
instead of icon
:
var markerToAdd = new OpenLayers.Marker(lonlat, icon);
Upvotes: 1