Ajax调用从ASP.NET MVC Controller获取GeoJson数据

时间:2022-10-18 09:37:46

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.

使用ASP.NET MVC 3和C#我有一个网页来显示一个地图,我想在其上添加一个由多个纬度和经度坐标组成的折线。使用Leaflet JavaScript库,您可以添加GeoJson图层。我想从C#中的数据库中获取经度和纬度坐标,并将坐标列表传递给JavaScript以创建GeoJson或GeoJson。

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

这是我想创建的GeoJson的一个例子:

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:

如何创建类似于上面显示的G​​eoJson并将位置数据添加到C#或JavaScript的“coordinates”部分,然后在JavaScript中使用它来添加图层:

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

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

我已经开始使用GeoJSON.net并提出了这个代码:

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:

但我不知道如何将这个GeoJSON LinseString对象从C#传递给JavaScript。我无法使用Json传递它:

return Json(data, JsonRequestBehavior.AllowGet);

2 个解决方案

#1


4  

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):

简单地看一下GeoJSON.NET并使用JSON.NET,因此在返回结果时需要使用JSON.NET序列化程序(.NET中的JSON序列化程序不知道JSON.NET属性。)为此你可以像这样序列化并返回一个ContentResult(尚未测试过):

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.

或者更好,你可以使用自定义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.

另外,对于不符合GeoJSON规范的多边形序列化似乎存在问题 - 不确定这是否也会影响折线。但事实上,这一年没有修复,并不能保证GeoJSON库的好处。该项目似乎被放弃了。

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

我们选择在nettopologysuite中使用GeoJSON序列化,我记得它直接开箱即用。

#2


1  

I don't really have a magic bullet for this but I believe I can at least point you in the right direction.

我真的没有这个神奇的子弹,但我相信我至少能指出你正确的方向。

If you happen to be using PostGres/PostGIS, you could use the ST_AsGeoJson function to just return GeoJson straight from the database, which is handy. Otherwise you'll want to start out by looking into JSON.NET, which is the defacto standard JSON serialization library for ASP MVC. I found it to be a bit full-on, and I've not delved into it deep enough to suggest how you might proceed beyond just gaining a bit of familiarity.

如果您正好使用PostGres / PostGIS,您可以使用ST_AsGeoJson函数直接从数据库返回GeoJson,这很方便。否则,您将首先查看JSON.NET,它是ASP MVC的事实标准JSON序列化库。我发现它有点全面,而且我没有深入研究它,以便建议你如何继续进行,而不仅仅是获得一点熟悉。

Also, it appears that there's a GeoJson plugin for JSON.NET which has a Nuget Package and corresponding GitHub repo. I've not used it personally so I can't exactly vouch for it's stability/feature set etc etc, but in any event it might be a good jumping off point.

此外,似乎有一个JSON.NET的GeoJson插件,它有一个Nuget包和相应的GitHub repo。我没有亲自使用它,所以我不能保证它的稳定性/功能集等等,但无论如何它可能是一个很好的跳跃点。

Hope this helps, and I'd be interested in hearing what you end up going with!

希望这有所帮助,我有兴趣听听你最终的结果!

#1


4  

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):

简单地看一下GeoJSON.NET并使用JSON.NET,因此在返回结果时需要使用JSON.NET序列化程序(.NET中的JSON序列化程序不知道JSON.NET属性。)为此你可以像这样序列化并返回一个ContentResult(尚未测试过):

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.

或者更好,你可以使用自定义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.

另外,对于不符合GeoJSON规范的多边形序列化似乎存在问题 - 不确定这是否也会影响折线。但事实上,这一年没有修复,并不能保证GeoJSON库的好处。该项目似乎被放弃了。

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

我们选择在nettopologysuite中使用GeoJSON序列化,我记得它直接开箱即用。

#2


1  

I don't really have a magic bullet for this but I believe I can at least point you in the right direction.

我真的没有这个神奇的子弹,但我相信我至少能指出你正确的方向。

If you happen to be using PostGres/PostGIS, you could use the ST_AsGeoJson function to just return GeoJson straight from the database, which is handy. Otherwise you'll want to start out by looking into JSON.NET, which is the defacto standard JSON serialization library for ASP MVC. I found it to be a bit full-on, and I've not delved into it deep enough to suggest how you might proceed beyond just gaining a bit of familiarity.

如果您正好使用PostGres / PostGIS,您可以使用ST_AsGeoJson函数直接从数据库返回GeoJson,这很方便。否则,您将首先查看JSON.NET,它是ASP MVC的事实标准JSON序列化库。我发现它有点全面,而且我没有深入研究它,以便建议你如何继续进行,而不仅仅是获得一点熟悉。

Also, it appears that there's a GeoJson plugin for JSON.NET which has a Nuget Package and corresponding GitHub repo. I've not used it personally so I can't exactly vouch for it's stability/feature set etc etc, but in any event it might be a good jumping off point.

此外,似乎有一个JSON.NET的GeoJson插件,它有一个Nuget包和相应的GitHub repo。我没有亲自使用它,所以我不能保证它的稳定性/功能集等等,但无论如何它可能是一个很好的跳跃点。

Hope this helps, and I'd be interested in hearing what you end up going with!

希望这有所帮助,我有兴趣听听你最终的结果!