加载GeoJson文件来制作传单 [英] Load GeoJson file for Leaflet choropleth
问题描述
我有一个GeoJson文件,我正在尝试将该文件加载到Leaflet的logic.js中.我在console.logged响应中获取了数据,但是,当我尝试将数据应用于地图时,它给了我以下错误:Uncaught (in promise) Error: Invalid GeoJSON object.
我的GeoJSON数据如下所示:
I have a GeoJson file that I am trying to load to logic.js for the Leaflet. I console.logged the response and it fetches the data, however, when I try to apply the data onto the map, it gives me the following error: Uncaught (in promise) Error: Invalid GeoJSON object.
My GeoJSON data looks like this:
"type": "WineCollection",
"wines": [
{
"type": "wine",
"properties": {
"country": "Albania",
"points": 88.0,
"price": 20.0
},
"geometry": {
"type": "Point",
"coordinates": [
19.9999619,
41.000028
]
}
},
这是我的代码:
center: [40.7128, -74.0059],
zoom: 2.5
});
// Adding tile layer
L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}", {
attribution: "Map data © <a href=\"https://www.openstreetmap.org/\">OpenStreetMap</a> contributors, <a href=\"https://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>, Imagery © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.streets",
accessToken: API_KEY
}).addTo(myMap);
d3.json("static/js/wine.json").then(function(data) {
L.geoJson(data).addTo(myMap);
});
推荐答案
数据样本不符合 GeoJSON规范.
您可以拥有一个"FeatureCollection"类型的对象,并具有一个"features"数组,这些数组是"Feature"类型的对象(并且像您的数据样本一样具有"properties"和"geometry").
You can have an object of type "FeatureCollection", having an array of "features", which are objects of type "Feature" (and having "properties" and a "geometry" like in your data sample).
很遗憾,您无法自定义这些类型.
Unfortunately you cannot customise those types.
有几种可用的GeoJSON linting工具可以帮助您快速发现不合规的数据,例如 http://geojsonlint.com/
There are several GeoJSON linting tool available that can help you quickly spot non compliant data, e.g. http://geojsonlint.com/
这篇关于加载GeoJson文件来制作传单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!