[英]GeoJSON .NET with Google Maps in ASP.NET MVC
I am trying to create and load geoJSON data into Google Maps using the GeoJSON .NET library using ASP.NET MVC5 though I am doing something wrong somewhere. 我正在尝试使用ASP.NET MVC5使用GeoJSON .NET库创建geoJSON数据并将其加载到Google Maps中,尽管我在某处做错了。
Using the example code posted on the GitHub site my controller looks like this: 使用GitHub站点上发布的示例代码,我的控制器如下所示:
public ActionResult GetGeoJson()
{
var point = new GeoJSON.Net.Geometry.Point(
new GeoJSON.Net.Geometry.GeographicPosition(45.79012, 15.94107));
var featureProperties = new Dictionary<string, object> { {"Name", "Foo"} };
var model = new GeoJSON.Net.Feature.Feature(point, featureProperties);
var serializedData = JsonConvert.SerializeObject(model, Formatting.Indented,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
return Json(serializedData, JsonRequestBehavior.AllowGet);
}
And my view is as follows: 我的观点如下:
@{
ViewBag.Title = "Map";
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=foobar"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
function initialize() {
var centerPoint = new google.maps.LatLng(53.710921, -1.626776);
var mapOptions = {
center: centerPoint,
zoom: 12
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
map.data.loadGeoJson('/Home/GetGeoJson');
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="map-canvas">
</div>
When I load the page and check the console in chrome I have the following error: 当我加载页面并在chrome中检查控制台时,我有以下错误:
Uncaught InvalidValueError: not a Feature or FeatureCollection
Uncaught InvalidValueError:不是Feature或FeatureCollection
If I go to my Action within the brower it outputs the following: 如果我在浏览器中进入我的行动,它输出以下内容:
"{\r\n \"geometry\":
{\r\n \"coordinates\": [\r\n 15.94107,\r\n 45.79012\r\n ],
\r\n \"type\": \"Point\"\r\n },
\r\n \"properties\": {\r\n \"name\": \"Foo\"\r\n },
\r\n \"type\": \"Feature\"\r\n}"
A related SO question gave me the answer. 一个相关的SO 问题给了我答案。 I needed to change the return in my controller from
我需要更改控制器的返回值
return Json(serializedData, JsonRequestBehavior.AllowGet);
to 至
return Content(serializedData, "application/json");
as the JsonResult was also serializing the serialized data. 因为JsonResult也在序列化序列化数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.