簡體   English   中英

leaflet.js:未捕獲的錯誤:無效的LatLng對象:(NaN,NaN)

[英]leaflet.js: Uncaught Error: Invalid LatLng object: (NaN, NaN)

我有一個需要兩個坐標的json文件

"_id" : ObjectId("59407457838b932e0677999e"),
"type" : "MultiPoint",
"name" : "points",
"coordinates" : [
        [
                -73.958,
                40.8003
        ],
        [
                -73.9814,
                40.7681
        ]
]

我正在使用Leaflet庫使用node js和mongo db在Google地圖中標記坐標。

我的map.pug文件是

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文件,則不會顯示該文件。

"_id" : ObjectId("594061ea838b932e0677999d"),
        "type" : "MultiPoint",
        "name" : "points",
        "coordinates" : [
                -73.958,
                40.8003
        ]

瀏覽器控制台中的錯誤是

leaflet.js:6 Uncaught Error: Invalid LatLng object: (NaN, NaN)

誰能解釋一下如何解決此問題並在地圖上顯示一個坐標?

GeoJSON MultiPoint幾何類型期望將coordinates作為嵌套位置數組 每個“位置”是2個值的數組,即[longitude, latitude] (海拔可能是第三位)

如果僅提供一個位置而不是嵌套在父數組中,則您的幾何不再符合GeoJSON。

相反,您還應該將幾何類型更改為Point ,它確實希望將coordinates作為一個lng-lat位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM