首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >leaflet.js:不明错误:无效的LatLng对象:(NaN,NaN)

leaflet.js:不明错误:无效的LatLng对象:(NaN,NaN)
EN

Stack Overflow用户
提问于 2017-06-13 23:35:38
回答 1查看 4.2K关注 0票数 1

我有一个json文件,它接受两个坐标

代码语言:javascript
运行
复制
"_id" : ObjectId("59407457838b932e0677999e"),
"type" : "MultiPoint",
"name" : "points",
"coordinates" : [
        [
                -73.958,
                40.8003
        ],
        [
                -73.9814,
                40.7681
        ]
]

我使用传单库来标记Google地图中的坐标,使用节点js和mongo。

我的map.pug文件是

代码语言:javascript
运行
复制
extends layout.pug

block content
    #map
    script(type='text/javascript').
        var map = L.map('map').setView([#{lat},#{lng}], 14);
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);

        $.getJSON('/maplayers',function(result){
            $.each(result, function(i, mlayer){
                $.getJSON('/mapjson/' + mlayer.name, function(data) { addLayer(data, mlayer.name ) });
            });
        });

        function addLayer(layer, name) {
            var leaf_layer;
            if (layer.type == "MultiPoint") {
                leaf_layer = L.geoJson(layer, { pointToLayer: function (feature, latlng) {return L.circleMarker(latlng, layer.style); }})
                leaf_layer.bindPopup(layer.type);
            } 
            leaf_layer.addTo(map);

            $('#' + name).click(function(e) {

                if (map.hasLayer(leaf_layer)) {
                    map.removeLayer(leaf_layer);
                } else {
                    map.addLayer(leaf_layer);
                }
            });
        }

这些值与浏览器(http://localhost:3000/map)中的标记一起显示在地图上。

但是,如果我用一个坐标像下面这样更改Json文件,就不会显示它。

代码语言:javascript
运行
复制
"_id" : ObjectId("594061ea838b932e0677999d"),
        "type" : "MultiPoint",
        "name" : "points",
        "coordinates" : [
                -73.958,
                40.8003
        ]

浏览器控制台中的错误为

代码语言:javascript
运行
复制
leaflet.js:6 Uncaught Error: Invalid LatLng object: (NaN, NaN)

有谁能解释一下如何解决这个问题,用一个坐标在地图上显示吗?

EN

回答 1

Stack Overflow用户

发布于 2017-06-14 02:28:27

GeoJSON MultiPoint几何类型期望coordinates作为嵌套位置的数组。每个“位置”是一个由2个值组成的数组,即[longitude, latitude]。(海拔可能在第三位)

如果只提供一个位置,而不是嵌套在父数组中,则您的几何图形不再兼容GeoJSON。

相反,您还应该将几何类型更改为Point,这确实希望coordinates作为一个lat位置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44533161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档