简体   繁体   English

导入数据geojson的传单变量

[英]leaflet variable for import data geojson

I just used a leaflet. 我只是用了一张传单。 From the geoJSON demo page I saw if you want to include data you have to use 在geoJSON演示页面上,我看到了是否要包含必须使用的数据

<script src="data/us-states.geojson"></script>

and if you open the files looks like 如果打开文件,看起来像

var ustates = {
"type": "FeatureCollection",
......
[]
};

and if you want to call them use 如果您想打电话给他们,请使用

var data = [ustates] ;

Is there another way to call the data? 还有另一种调用数据的方法吗? The geojson file that I have has no initial variable and looks like this: 我没有的geojson文件没有初始变量,如下所示:

{
"type": "FeatureCollection",
......
[]
}

I have lots of data and I have to open 1 by 1 to add variable on geojson data so I mean can I just call the data like 我有很多数据,我必须打开1来在geojson数据上添加变量,所以我可以像这样调用数据

var ustates = <?php include "data/us-states.geojson"; ?>
var data = [ustates];

You can use follow this link 您可以使用以下链接

function fetchJSON(url) {
  return fetch(url)
    .then(function(response) {
      return response.json();
    });
}


var data = fetchJSON('data/us-states.geojson');

There's a handy plugin which will help with this - leaflet-ajax - which you can find here https://github.com/calvinmetcalf/leaflet-ajax . 有一个方便的插件可以帮助您解决此问题-leaflet-ajax-您可以在这里找到https://github.com/calvinmetcalf/leaflet-ajax This will handle fetching the file and creating a Leaflet layer from it in one easy step. 通过一个简单的步骤,这将处理获取文件并从中创建一个Leaflet层的过程。

Example usage: 用法示例:

var geojsonlayer = new L.GeoJSON.AJAX("data/us-states.json").addTo(map);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM