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

后端 未结 2 1944
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 19:35

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 Lea

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-08 20:11

    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.

提交回复
热议问题