从geoJson获取参数

时间:2013-05-14 10:14:55

标签: javascript openlayers geojson

您好我使用以下代码从我的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}}

1 个答案:

答案 0 :(得分:0)

实际上问题是,我无法读取图层的功能,因为它还没有完全加载(异步调用)。它可以通过在“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?