【发布时间】:2014-03-31 12:49:37
【问题描述】:
我正在使用 Node/express.js 加载 JSON 数据并将其绘制在地图上。作为第一个开始,我受到 repp 传单-geojson-stream 中提供的示例的启发。 https://github.com/tmcw/leaflet-geojson-stream/tree/master/example
客户: https://github.com/tmcw/leaflet-geojson-stream/blob/master/example/client.js
var L = require('leaflet'),
leafletStream = require('../');
L.Icon.Default.imagePath = 'http://leafletjs.com/dist/images';
window.onload = function() {
var div = document.body.appendChild(document.createElement('div'));
div.style.cssText = 'height:500px;';
var map = L.map(div).setView([0, 0], 2);
L.tileLayer('http://a.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
var gj = L.geoJson().addTo(map);
leafletStream.ajax('/points.geojson', gj)
.on('end', function() {
});
};
服务器: https://github.com/tmcw/leaflet-geojson-stream/blob/master/example/server.js
var express = require('express'),
browserify = require('browserify'),
app = express();
app.get('/', function(req, res) {
res.send('<html><head><link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" /></head><body><script src="bundle.js"></script></html>');
});
app.get('/bundle.js', function(req, res) {
var b = browserify();
b.add('./client.js');
b.bundle().pipe(res);
});
app.get('/points.geojson', function(req, res) {
res.write('{"type":"FeatureCollection","features":[');
var i = 0, die = 0;
function send() {
if (++i > 20) {
res.write(JSON.stringify(randomFeature()) + '\n,\n');
i = 0;
} else {
// it seems like writing newlines here causes the buffer to
// flush
res.write('\n');
}
if (die++ > 1000) {
res.write(JSON.stringify(randomFeature()));
res.write(']');
res.end();
return;
}
setTimeout(send, 10);
}
send();
});
app.listen(3000);
function randomFeature() {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [
(Math.random() - 0.5) * 360,
(Math.random() - 0.5) * 180
]
},
properties: {}
};
}
在项目中,他们创建随机的 json 文件。我想读取 json 文件,然后绘制它。我想“流数据”的原因是处理文件的大小(我知道有更好更简单的方法来加载 json 数据),但我想使用这个模块。
我修改了服务器脚本:
var express = require('express'),
browserify = require('browserify'),
app = express();
app.get('/', function(req, res) {
res.send('<html><head><link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" /></head><body><script src="bundle.js"></script></html>');
});
app.get('/bundle.js', function(req, res) {
var b = browserify();
b.add('./client.js');
b.bundle().pipe(res);
});
var o = require('../output_.geojson');
app.get('/points.geojson', function(req, res) {
res.json(o);
});
app.listen(3000);
res.write('');
但我收到错误:
/Users/macbook/leaflet-geojson-stream/output_.geojson:1
(function (exports, require, module, __filename, __dirname) { "type":"FeatureC
^
SyntaxError: Unexpected token :
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/macbook/leaflet-geojson-stream/example/server.js:15:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
谁能提示我应该怎么做才能读取和加载 json 外部文件以绘制数据。
【问题讨论】:
标签: javascript json node.js leaflet