Pass list item from C # to javascript array
I have the following code to show multiple markers in gmap
<script type="text/javascript"> function init() { var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(-33.92, 151.25), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addListener(marker, 'click', (function (marker, i) { return function () { infowindow.setContent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } return false; } </script> I want to make this dynamic, so I need to pass this
var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; from c # code to this js.
I tried hidden field and this code
List<String> oGeocodeList = new List<String> { "'Bondi Beach', -33.890542, 151.274856, 4", "'Coogee Beach', -33.923036, 151.259052, 5", "'Cronulla Beach', -34.028249, 151.157507, 3", "'Manly Beach', -33.80010128657071, 151.28747820854187, 2", "'Maroubra Beach', -33.950198, 151.259302, 1" }; var geocodevalues = string.Join(",", oGeocodeList.ToArray()); ClientScript.RegisterArrayDeclaration("locations", geocodevalues); But I will not be able to find any link
Using Json.Net
var obj = new[] { new object[] { "Bondi Beach", -33.890542, 151.274856, 4 }, new object[] { "Coogee Beach", -33.923036, 151.259052, 5 }, new object[] { "Cronulla Beach", -34.028249, 151.157507, 3 }, new object[] { "Manly Beach", -33.80010128657071, 151.28747820854187, 2 }, new object[] { "Maroubra Beach", -33.950198, 151.259302, 1 }, }; var json = JsonConvert.SerializeObject(obj); Essentially, I believe that you are trying to create a JSON string. JSON allows you to transfer objects in a sequential string format between many languages, including JavaScript and C #. I would recommend taking a look at the JSON.NET library . This is a fantastic library that will allow you to safely and efficiently serialize elements to and from strings in C #.
I would also recommend creating a more OOP structure instead of passing a multidimensional array. For this you want to create a class for your locations, I will assume the following:
public class Location { public string Name { get; set; } public double Lat { get; set; } public double Lng { get; set; } } Then you want to build a List<Location> , and then serialize it using the JSON.NET library, which will be just as simple:
List<Location> oGeocodeList = new List<Location>() { //... }; string json = JsonConvert.SerializeObject(oGeocodeList); With this JSON, you will either want to write it to a hidden field, or to a variable in JavaScript. This will allow you to link to it on the page via JavaScript. There is also a fairly complete documentation , which is very useful!
In this case, you can access your JavaScript like any other js object, for example:
for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i].Lat, locations[i].Lng), map: map }); // ... } you should use it something like this: in your controller:
var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var locationsString = JsonConvert.SerializeObject(locations); ViewBag.locationsString = locationsString; return View(); and in your java script use it like this:
var data = JSON.parse(@ViewBag.locationsString); and you can have your data in an array and use it in javascript of each function:
$.each(data, function (i, item) { //enter your code } to use javascript not jquery:
for (var i = 0; i < data.length; i++) { addMarker(data[i], map); }