Ajax call to get GeoJson data from ASP.NET MVC Con

2019-03-19 16:32发布

问题:

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

回答1:

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.



回答2:

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.

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.

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