【发布时间】:2011-11-03 15:41:21
【问题描述】:
我想将 VIEWBAG 中的值作为 LatLons 数组从我的 C# 模型中传递。我不确定如何动态循环 Viewbag 数组并将这些坐标添加到我的 javascript 数组以传递给:
// Create an instance of Google map
var map = new GMap2(document.getElementById("map"));
// Tell the map where to start
map.setCenter(new GLatLng(59.3324, 17.8857), 9);
// Create an array with points
var points = [
new GLatLng(59.6919, 17.8582),
new GLatLng(59.3030, 18.0395),
new GLatLng(58.9789, 17.5341)
];
// Create a new polyline
var polyline = new GPolyline(points, '#ff0000', 5, 0.7);
// Add the polyline to the map using map.addOverlay()
map.addOverlay(polyline);
我想做类似上面的事情,但没有静态数组。
提前致谢!
编辑:
目前我有:
var points = [];
@foreach (var item in ViewBag.LatLons)
{
<text>
points.push(new GLatLng(@item.Latitude, @item.Longitude);
</text>
}
但是添加这个时谷歌地图不会显示,但是从viewbag数据中正确地迭代了点。
【问题讨论】:
-
这一行:points.push(new GLatLng(@item.Latitude, @item.Longitude); 应该是:points.push(new GLatLng(@item.Latitude, @item.Longitude)) ;
标签: c# .net asp.net-mvc-3 google-maps-api-3