我有以下代码在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>
我想让这个动态,所以我必须通过这个
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]
];
从c#代码到这个js。
我试过Hidden Field和这段代码
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);
但没有运气,任何参考都会对我有所帮助
答案 0 :(得分:5)
我认为你能做的就是创建一个List。
示例:
List<T> data = new List<T>();
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(data);
多数民众赞成。
我希望它有用
答案 1 :(得分:4)
基本上我认为你要做的是创建一个JSON
字符串。 JSON
允许您在多种语言(包括JavaScript和C#)之间以序列化字符串格式传递对象。我建议你看看JSON .NET library。它是一个非常棒的库,可以让您安全有效地将项目序列化为C#中的字符串。
我还建议您创建更多OOP结构,而不是传递多维数组。为此,您需要为您的位置创建一个类,我将假设以下内容:
public class Location
{
public string Name { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
}
然后,您将构建一个List<Location>
,然后使用JSON .NET库对其进行序列化,这将非常简单:
List<Location> oGeocodeList = new List<Location>() {
//...
};
string json = JsonConvert.SerializeObject(oGeocodeList);
使用此JSON,您可能要将其写入隐藏字段或JavaScript中的变量。这将允许您通过JavaScript在您的页面上引用它。还有pretty comprehensive documentation,证明非常有用!
然后可以在JavaScript中像任何其他js对象一样访问它,如下所示:
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
});
// ...
}
答案 2 :(得分:2)
使用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);
答案 3 :(得分:1)
答案 4 :(得分:1)
你必须使用这样的东西: 在你的控制器中:
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();
并在您的java脚本中使用它:
var data = JSON.parse(@ViewBag.locationsString);
你可以将你的数据放在数组中并在每个函数的javascript中使用它:
$.each(data, function (i, item) {
//enter your code
}
对于javascript而不是jquery使用:
for (var i = 0; i < data.length; i++) {
addMarker(data[i], map);
}