OpenLayers - 如何验证BBOX策略是否成功运行?

时间:2012-08-24 08:20:32

标签: openlayers

我想向我们的要素服务器发送请求,该请求只询问其中的数据 可视地图范围。所以我使用了BBOX策略和HTTP协议 以下代码。

    mVectorLayer = new OpenLayers.Layer.Vector("Overlay", {
        strategies: [new OpenLayers.Strategy.BBOX()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: 'http://localhost:56786/jlist.geojson',
            format: new OpenLayers.Format.GeoJSON({
                'read': myReadFunction,
                'internalProjection': map.baseLayer.projection,
                'externalProjection': new OpenLayers.Projection("EPSG:4326")
            })
        }),
        projection: new OpenLayers.Projection("EPSG:900913")
    });

我在可查看地图之外添加了一个功能到下面显示的geojson文件。

{ "type": "FeatureCollection",
  "features": [
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [29.0, 41.060]},
        "properties": {"name": "IST J1", "img": "img/marker.png"}
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [29.0, 41.100]},
        "properties": {"name": "IST J2", "img": "img/marker.png"}
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [59.0, 41.100]},
        "properties": {"name": "IST J3", "img": "img/marker.png"}
      }
  ]
}

为了验证,我已经向myReadFunction函数添加了一个警告 json字符串。但警报显示geojson文件中的所有功能。一世 假设我们的功能服务器发送所有geojson内容而不是可查看 特征?如何验证或观察BBOX策略是否成功运行?

        function myReadFunction(json, type, filter) {
            alert("json: " + json);

            type = (type) ? type : "FeatureCollection";
            var results = null;
            var obj = null;
            if (typeof json == "string") {
                obj = OpenLayers.Format.JSON.prototype.read.apply(this, [json, filter]);
            } else {
                obj = json;
            }
            if (!obj) {
                OpenLayers.Console.error("Bad JSON: " + json);
            } else if (typeof (obj.type) != "string") {
                OpenLayers.Console.error("Bad GeoJSON - no type: " + json);
            } else if (this.isValidType(obj, type)) {
                switch (type) {
                    case "Geometry":
                        try {
                            results = this.parseGeometry(obj);
                        } catch (err) {
                            OpenLayers.Console.error(err);
                        }
                        break;
                    case "Feature":
                        try {
                            results = this.parseFeature(obj);
                            results.type = "Feature";
                        } catch (err) {
                            OpenLayers.Console.error(err);
                        }
                        break;
                    case "FeatureCollection":
                        // for type FeatureCollection, we allow input to be any type
                        results = [];
                        switch (obj.type) {
                            case "Feature":
                                try {
                                    results.push(this.parseFeature(obj));
                                } catch (err) {
                                    results = null;
                                    OpenLayers.Console.error(err);
                                }
                                break;
                            case "FeatureCollection":
                                for (var i = 0, len = obj.features.length; i < len; ++i) {
                                    try {results.push(this.parseFeature(obj.features[i]));
                                    } catch (err) {
                                        results = null;
                                        OpenLayers.Console.error(err);
                                    }
                                }
                                break;
                            default:
                                try {
                                    var geom = this.parseGeometry(obj);
                                    results.push(new OpenLayers.Feature.Vector(geom));
                                } catch (err) {
                                    results = null;
                                    OpenLayers.Console.error(err);
                                }
                        }
                        break;
                }
            }
            return results;
}

非常感谢您的帮助和解释, Yasemin

1 个答案:

答案 0 :(得分:0)

如果您在Firefox中使用类似firebug的内容,则可以在“网络”标签中查看所有网页请求。您可以查看传输的参数(您的边界)和结果集。这样您就可以检查服务器响应是否正常。

但你应该知道BBOX有一个比例。这是边界扩展的因子,因此数据边界与视口边界的比率。默认值是2!因此,如果您的测试功能位于视口之外,它将位于请求的bbox中。

http://dev.openlayers.org/docs/files/OpenLayers/Strategy/BBOX-js.html#OpenLayers.Strategy.BBOX.ratio