您好,我正在使用以下代码从我的 java 服务器应用程序加载 geoJson 文件。它工作没有问题,我可以在 openlayers 中将 geojson 显示为矢量图层。但在服务器端,我还将一些参数保存到 geoJson 文件中,稍后我想在客户端 javaScript 端读取这些参数(例如路径的总长度和路径的总时间。)我不知道如何访问它们.
这就是我所拥有的,并且有效的:
layer = new OpenLayers.Layer.Vector("Path", {
style: style,
strategies: [new OpenLayers.Strategy.Fixed()],
projection: geographic,
protocol: new OpenLayers.Protocol.HTTP({
url: "webresources/getJosmAspects?startLon=" + document.getElementById('startLon').value +
"&startLat=" + document.getElementById('startLat').value +
"&endLon=" + document.getElementById('endLon').value +
"&endLat=" + document.getElementById('endLat').value +
"&avgSpeed=" + document.getElementById('avgSpeed').value +
"&speedWeight=" + document.getElementById('speedInput').value +
"&comfortWeight=" + document.getElementById('comfortInput').value +
"&quietnessWeight=" + document.getElementById('quietInput').value +
"&avgSpeedWeight=" + document.getElementById('shortestInput').value,
format: new OpenLayers.Format.GeoJSON()
})
});
我想从响应中获取一些参数,而无需再次调用它。 来自服务器的 geojson 看起来像
{"type":"Feature","geometry":{"type":"LineString","coordinates":[[14.420414,50.07352]......,[14.38296,50.077154],[14.382867,50.077228]]},"properties":{"total_length":3603.656606483625,"total_time":782.4648477846279,"total_elevationGain":130.9226633310318,"total_elevationDrop":-48.82620286941528}}
最佳答案
实际上问题是,我无法读取该层的功能,因为它尚未完全加载(异步调用)。可以通过在“loadend”事件上添加事件监听器来解决。这是有效的:
layer.events.register("loadend", layer, function() {
window.alert(layer.features[layer.features.length - 1].attributes.total_time);
});
这里讨论:How to get the features in the kind of vector layer which is created by using protocol?
关于javascript - 从 geoJson 获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16540531/