我想發送請求到我們的功能服務器,只詢問 中可見的地圖範圍內的數據。所以我使用BBOX策略和HTTP協議作爲 以下代碼。OpenLayers - 如何驗證BBOX策略是否成功運行?
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文件中的所有功能。 I 假設我們的功能服務器發送所有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