leaflet.js:6未捕获的TypeError:无法读取属性' lat'为null

时间:2017-09-29 09:23:26

标签: javascript json asp.net-mvc


我开发了一个示例网页,可以查看开放的街道地图,并通过Json(javascript)来读取sql server数据库中的数据
让我这个错误
leaflet.js:6未捕获的TypeError:无法读取属性' lat'为null 函数从数据库中成功返回数据

 模型文件
这里通过json返回数据

        [HttpPost]
    public JsonResult GetMap()
    {
        var data1 = (from p in db.Map
                     select new
                     {
                         Name = p.Name,
                         Latitude = p.Latitude,
                         Logitude = p.Logitude,
                         Location = p.Location,
                         Description = p.Description,
                         Id = p.Id
                     }).ToList().Select(res => new Map
                {
                    Name = res.Name,
                    Latitude = res.Latitude,
                    Logitude = res.Logitude,
                    Location = res.Location,
                    Description = res.Description,
                    Id = res.Id


                });
        return Json(data1, JsonRequestBehavior.AllowGet);

    } 


 查看文件 这里从json函数获取数据并成功获取数据

 <body>
<div id="mapid" style="height:600px"></div>
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
    $(document).ready(function () {

    var map = L.map('mapid').setView([31.291340, 34.244190], 13);

    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);




        $.ajax({
            type: "POST",
            url: '/Maps/GetMap',
            success: function (data) {
                //var result = JSON.stringify(data);
                var result = data;
                for (var i = 0; i < data.length; ++i) {

                    var popup =
                        '<b>Name:</b> ' + data[i].Name
                        +
                     '<br/><b>Latitude:</b> ' + data[i].Latitude +
                       '<br/><b>Longitude:</b> ' + data[i].Logitude +
                    '<br/><b>Location:</b> ' + data[i].Location
                    ;

                    alert(data[i].Name + " " + data[i].Latitude + " / " + data[i].Logitude + " / " + data[i].Location);
                    L.marker(data[i].Logitude,  [data[i].Latitude])

                        .bindPopup(popup)
                       .addTo(map); 

                }

            },
            error: function (xhr) {

                console.log(xhr.responseText);
                alert("Error has occurred..");
            }
        });
    });

</script>

1 个答案:

答案 0 :(得分:0)

实际上它是一个linq查询,它们是deferred execution,这意味着它只会在我们要使用它时执行,所以你只是将0 => "082016" 1 => "092016" 2 => "102016" 3 => "112016" 4 => "122016" 5 => "012017" 6 => "032017" 7 => "042017" 8 => "052017" 9 => "062017" 10 => "072017" 11 => "082017" 对象传递给IQueryable方法和它返回的时间没有实现结果。

传递给JSON方法时,您需要使用ToList()方法实现结果。

只需将您的最后一行更改为:

JSON

现在 EF 将在数据库服务器上执行查询,并将获取结果并填充return Json(data1.ToList(), JsonRequestBehavior.AllowGet); 对象。

希望它有所帮助!

相关问题