Ajax 调用从 ASP.NET MVC 控制器获取 GeoJson 数据 [英] Ajax call to get GeoJson data from ASP.NET MVC Controller

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

问题描述

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

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.

这是我希望创建的 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
      }
};

如何创建类似于上图所示的 GeoJson,并从 C# 或 JavaScript 将位置数据添加到坐标"部分,然后在 JavaScript 中使用它来添加这样的层:

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

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

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

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

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

推荐答案

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

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

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

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

附带说明,似乎多边形序列化不符合 GeoJSON 规范的问题 - 不确定这是否也会影响折线.但这一问题一年后仍未得到解决,这对 GeoJSON 库来说并不好.该项目似乎被放弃了.

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.

我们选择在 nettopologysuite 中使用 GeoJSON 序列化,开箱即用我记得.

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天全站免登陆