简体   繁体   English

使用边界框策略加载Open Layers 3 jsonp矢量层吗?

[英]Load Open layers 3 jsonp vector layer with bounding box strategy?

I have problem with loading features from geoserver to a vector layer using OpenLayers3 and bounding box strategy. 使用OpenLayers3和边界框策略将要素从Geoserver加载到矢量层时遇到问题。 I tried to find how I can load more than one layer using bounding box strategy, but without success. 我试图找到如何使用边界框策略加载多个图层的方法,但是没有成功。 The only example which I found is with one layer and using global functions, which in my case is not applicable ( http://acanimal.github.io/thebookofopenlayers3/chapter03_08_loading_strategies.html ). 我发现的唯一示例是一层并且使用了全局函数,在我的情况下这是不适用的( http://acanimal.github.io/thebookofopenlayers3/chapter03_08_loading_strategies.html )。 The problem is that the function, which is loading the response, is not defined globally - if so I'll have to create such function for every single layer, which I want to load, right? 问题是正在加载响应的函数未全局定义-如果是这样,我将不得不为我要加载的每个图层创建此类函数,对吗? This is an example url address for requesting features: 这是请求功能的示例网址:

http://192.168.1.10/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature&typename=ubutrusty:places&outputFormat=text/javascript&format_options=callback:success&srsname=EPSG:32635&bbox=161473.81383919955,4698323.564696768,234672.52335922938,4767981.6354873795,EPSG:32635 

You can see that format_options parameter is set to callback:success and I'm sure this is the problem, but I'm struggling with setting the correct callback function. 您可以看到format_options参数设置为callback:success ,我敢肯定这是问题所在,但是我在设置正确的回调函数方面很挣扎。 My knowledge in javascript is not so good, so the question is not an easy one for me. 我对javascript的了解不是很好,所以这个问题对我来说并不容易。

This is how I'm trying to create a new vector layer: 这就是我试图创建一个新的矢量层的方法:

var layer = {
    name: 'ubutrusty:places',
    title: 'Позиции',
    source: createVectorLayer(layer),
    type: 'operational',
    visible: true,
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#800000',
            width: 2
        })
    })
}

This function is called for every layer I want to add to the map - it returns ol.source.ServerVector object: 我要添加到地图的每个图层都会调用此函数-它返回ol.source.ServerVector对象:

MyApp.prototype.createVectorLayer = function(layerData){
    var vectorSource = new ol.source.ServerVector({
        format: new ol.format.GeoJSON(),
        loader: function(extent, resolution, projection) {
            extent = ol.proj.transformExtent(extent, projection.getCode(), ol.proj.get(layerData.srcEPSG).getCode());
            var url = config.proxyUrl + layerData.url + '?service=WFS&' +
                'version=1.1.0&request=GetFeature&typename=' + layerData.name + '&' + 'outputFormat=text/javascript' +
                '&format_options=callback:success&srsname=' + layerData.srcEPSG + '&bbox=' + extent.join(',') + ',' + layerData.srcEPSG;
            $.ajax({
                url: url,
                dataType: 'jsonp',
                success: function(data) {
                    this.addFeatures(this.readFeatures(data));
                },
                error: function (e) {
                    var wtf = e.status;
                }
            });
        },
        strategy: ol.loadingstrategy.bbox,
        projection: layerData.srcEPSG
    })

    return vectorSource;
}

MyApp.map.addLayer(new ol.layer.Vector(layer);

The question is, is it possible to define one function for loading features from more than one layer and if yes, how I can do this? 问题是,是否可以定义一个功能来从多个图层加载要素,如果可以,我该怎么做? If I don't specify the callback function, then the default one is used (for geoserver it is parseResponse ) which is also not defined. 如果我没有指定回调函数,则使用默认函数(对于geoserver为parseResponse ),该函数也未定义。 If more information is needed I can give additional parameters or code examples. 如果需要更多信息,我可以提供其他参数或代码示例。

Thanks 谢谢

In your URL, you already define the name of the callback function. 在您的URL中,您已经定义了回调函数的名称。 It is success , and is defined by '&format_options=callback:success&srsname=' . 它是success ,并且由'&format_options=callback:success&srsname=' If you want to use your createVectorLayer function for multiple layers, you will want to change that to '&format_options=callback:success.' + layerData.name.replace(':', '_') + '&srsName=' 如果要将createVectorLayer函数用于多个图层,则需要将其更改为'&format_options=callback:success.' + layerData.name.replace(':', '_') + '&srsName=' '&format_options=callback:success.' + layerData.name.replace(':', '_') + '&srsName=' . '&format_options=callback:success.' + layerData.name.replace(':', '_') + '&srsName=' Then you can create a registry of functions in the global object literal success , which you define once: 然后,您可以在全局对象字面量success创建函数注册表,您只需定义一次即可:

window.success = {};

You can then add the following code to your createVectorLayer function, which will create a callback function for each of your layers: 然后,可以将以下代码添加到createVectorLayer函数中,该函数将为每个图层创建一个回调函数:

success[layerData.name.replace(':', '_')] = function(response) {
  vectorSource.addFeatures(vectorSource.readFeatures(response));
};

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

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