创建动态折线与谷歌地图和C#.NET [英] Creating a dynamic Polyline with Google Maps and C# .net

查看:137
本文介绍了创建动态折线与谷歌地图和C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从VIEWBAG为LatLons从我的模式在C#中的数组传递值。我不知道如何动态循环Viewbag阵列,并添加这些cordinates到我的JavaScript数组传递到:

I want to pass values from the VIEWBAG as an array of LatLons from my Model in C#. I am not sure how to dynamically loop the Viewbag array and add these cordinates to my javascript array to pass to:

    // 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);

我想要做的事情就像上面,但没有静态数组。

I want to do something like the above, but without the static array.

在此先感谢!

编辑:

目前我有:

       var points = [];


   @foreach (var item in ViewBag.LatLons)
   {
     <text>
     points.push(new GLatLng(@item.Latitude, @item.Longitude);
     </text>
   }

不过,谷歌地图将不会显示在此添加,但是该点被正确地从viewbag数据迭代通过。

But the google map will not show up when this is added, However the points are correctly iterated through from the viewbag data.

推荐答案

相反,ViewBag的,如果你要使用视图模式,您可以使用类似以下内容:

Instead of the ViewBag, if you were to use a view model, you could use something like the following:

// Create an array with points
var points = [];
<% foreach (var item in Model.Coords)
   {%>
       points.push(new GLatLng(<%= item.lat %>, <%= item.lng %>));
   <%} %>

这应该输出到您的看法是:

which should output into your view as:

var points = [];
points.push(new GLatLng(59.6919, 17.8582));
points.push(new GLatLng(59.3030, 18.0395));
points.push(new GLatLng(58.9789, 17.5341));
//...et cetera

视图的数据本质上是一个对象,不支持迭代器,所以你将不得不投。这由具有类型化的集合保存需要浇铸在视图中。这个例子可以用一个简单的模型是这样的:

The view data is inherently an object and doesn't support an iterator, so you would have to cast. This saves the need for the cast in the view by having a typed collection. This example could use a simple model like this:

public class CoordsModel 
{
    public List<CoOrd> Coords {get; set;}
}

public class CoOrd 
{
    public decimal lat {get; set;}
    public decimal lng {get; set;}
}

这篇关于创建动态折线与谷歌地图和C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆