2

我想向我们的功能服务器发送请求,该服务器仅询问可视地图范围内的数据。所以我使用了 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

4

1 回答 1

0

如果您在 Firefox 中使用 firebug 之类的东西,您可以在“网络”选项卡中看到所有页面请求。您可以查看传输了哪些参数(您的界限)并偏离结果集。这样您就可以检查您的服务器响应是否正常。

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

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

于 2012-10-18T11:59:13.533 回答