AJAX调用从ASP.NET MVC控制器得到GeoJSON的数据 [英] Ajax call to get GeoJson data from ASP.NET MVC Controller

查看:277
本文介绍了AJAX调用从ASP.NET MVC控制器得到GeoJSON的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET MVC 3用C#我有一个网页,以显示地图上,我要添加包括几个纬度和经度坐标折线。随着宣传册 JavaScript库,你可以添加以GeoJSON 层。我想获得的经度和纬度,从C#中的数据库协调和坐标表传递到JavaScript来创建以GeoJSON 以GeoJSON

下面是我希望创建的以GeoJSON的例子:

  VAR折线= {
      类型:功能,
      几何体:{
      类型:LineString的,
                    坐标:[
                        [-105.00341892242432,39.75383843460583]
                        [-105.0008225440979,39.751891803969535] ...
                    ]
                },
      属性:{
      popupContent:这是许多坐标折线。
      underConstruction:假的
      }
};

如何创建以GeoJSON类似于上面显示和位置数据添加到坐标的从C#或JavaScript,然后部分使用它在JavaScript中添加图层这样:

  VAR myLayer = L.geoJson()AddTo就(图)。
myLayer.addData(折线);

我一直在使用GeoJSON.net起步,并没有想出这个code:

 的foreach(在位置定位点)
{
    coordinates.Add(点);
}GeoJSON.Net.Geometry.LineString线=新GeoJSON.Net.Geometry.LineString(坐标);串行的JavaScriptSerializer =新的JavaScriptSerializer();VAR数据= serializer.Serialize(线串);

但我不知道如何从C#对JavaScript传递这个以GeoJSON LinseString对象。我无法使用JSON传递它是这样:

 返回JSON(数据,JsonRequestBehavior.AllowGet);


解决方案

只是看着简要地GeoJSON.NET,它使用的 JSON.NET ,所以你需要你的时候你返回结果使用JSON.NET串行器(.NET中的JSON序列化并不了解JSON.NET属性)。要做到这一点你可以只序列化,并返回一个像这样的ContentResult类型(没有测试):

  VAR行=新GeoJSON.Net.Geometry.LineString(坐标);
JSON字符串= JsonConvert.SerializeObject(线);
返回内容(JSON,应用/ JSON);

或更好的,你可以使用的自定义ActionResult的JSON.NET

在一个侧面说明,似乎有问题与不与多边形符合系列化以GeoJSON规范 - 不知道这是否也影响折线。但事实证明,这并没有得到每年固定,不能用于以GeoJSON库的诺言。该项目似乎放弃了。

我们选择了使用以GeoJSON序列中 nettopologysuite ,该工作直出箱子的,我记得。

Using ASP.NET MVC 3 with C# I have a web page to display a map onto which I want to add a polyline consisting of several latitude and longitude coordinates. With the Leaflet JavaScript library you can add GeoJson layers. I want to get the longitude and latitude coordinates from a Database in C# and pass the list of coordinates to the JavaScript to create GeoJson or as GeoJson.

Here is an example of the GeoJson I wish to create:

var polyline = {
      "type": "Feature",
      "geometry": {
      "type": "LineString",
                    "coordinates": [
                        [-105.00341892242432, 39.75383843460583],
                        [-105.0008225440979, 39.751891803969535] …
                    ]
                },
      "properties": {
      "popupContent": "This is a polyline of many coordinates.",
      "underConstruction": false
      }
};

How can I create GeoJson similar to that shown above and add location data to the "coordinates" section from the C# or JavaScript and then use it in JavaScript to add a layer as such:

var myLayer = L.geoJson().addTo(map);
myLayer.addData(polyline);

I have started using GeoJSON.net and have come up with this code:

foreach (Position point in Positions)
{
    coordinates.Add(point);
}

GeoJSON.Net.Geometry.LineString line = new GeoJSON.Net.Geometry.LineString(coordinates);

JavaScriptSerializer serializer = new JavaScriptSerializer();

var data = serializer.Serialize(lineString);

But I do not know how to pass this GeoJSON LinseString object from the C# to the JavaScript. i was unable to pass it using Json as such:

return Json(data, JsonRequestBehavior.AllowGet);

解决方案

Just looked briefly at GeoJSON.NET and it uses JSON.NET, so you need to use the JSON.NET serializer when you return your result (the JSON serializer in .NET does not know about the JSON.NET attributes.) To do this you could just serialize and return a ContentResult like this (haven't tested this):

var line = new GeoJSON.Net.Geometry.LineString(coordinates);
string json = JsonConvert.SerializeObject(line);
return Content(json, "application/json");

or better you could use a custom JSON.NET ActionResult.

On a side note, there seems to be an issue with serialization of polygons not complying with the GeoJSON specification - not sure if this also affects polylines. But the fact that this has not been fixed a year on, does not promise good for a GeoJSON library. The project seems to be abandoned.

We opted for using the GeoJSON serialization in nettopologysuite, which worked straight out of the box as I remember.

这篇关于AJAX调用从ASP.NET MVC控制器得到GeoJSON的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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