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