2016-12-06 67 views
0

我正在尝试读取将向地图添加一些点的.geoJson文件。 我的数据文件lookes这样的:

{ 
"type": "FeatureCollection", 
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, 
"features": [ 
{ "type": "Feature", "properties": { "start": "1970", "end": "1972" },"geometry": { "type": "Point", "coordinates": [ 11.924550479340329, 55.94619451008279 ] } }, 
{ "type": "Feature", "properties": { "start": "1975", "end": "1976" }, "geometry": { "type": "Point", "coordinates": [ 12.06219628503271, 55.909617190392566 ] } }, { "type": "Feature", "properties": { "start": "1979", "end": "1980" }, "geometry": { "type": "Point", "coordinates": [ 12.06219628503271, 55.909617190392566 ] } }, 
{ "type": "Feature", "properties": { "start": "1980", "end": "1985" }, "geometry": { "type": "Point", "coordinates": [ 12.284822339303718, 55.639778377234506 ] } } 
] 
}; 

所以这是data.geojson。

正如你所看到的那样,有开始日期和结束日期,我需要使用它来把它放在时间线上,否则我只会使用addTo(地图),所有的点都会在那里。

,但我的代码来调用我的文件是:

var points = null; 
$.getJSON("data.geojson", function(data) { 
points = L.geoJson(data); 
}); 

尝试和测试,如果代码工作我做了“points.addTo(图)”而这一切都去我的屏幕上一片空白,我敢肯定,我的数据工作,因为当我写这样的代码:

$.getJSON("data.geojson", function(data) { 
points = L.geoJson(data).addTo(map); 
}); 

我可以看到所有的点。但通过这样做,我不能将我的数据用于正确的目的。 希望有人能帮助我解决这个问题。

+0

在你的第一个例子中,你如何以及何时调用'addTo(map)'?这感觉就像是一个异步问题。 –

+0

在第一个我需要的东西稍后,我将它添加到地图 像'var pointsToTimeline = L.timeline(points);' 'pointsToTimeline.addTo(map);' 但这不起作用 – JessieQuick

+0

This可能是问题..没有一个MVCE不能告诉:http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 –

回答

0

我通过使用d3.json来解决这个问题,导入我的数据并将其用于我想要的目的,结果证明是异步问题。 通过inporting D3的装载选项:

d3.select(window).on("load", init)

,然后使用此代码加载文件:

d3.json("data.geojson", function(data) { console.log(data); console.log(data[0]); points = data;

然后我所有的坐标系下得到了挂时间表。

相关问题