我有一个需要两个坐标的 json 文件
"_id" : ObjectId("59407457838b932e0677999e"),
"type" : "MultiPoint",
"name" : "points",
"coordinates" : [
[
-73.958,
40.8003
],
[
-73.9814,
40.7681
]
]
我正在使用传单库使用节点js和mongo db标记Google地图中的坐标。
我的地图文件是
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: '© <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 位置。