Validate that Openlayers GeoJSON layer is in the correct format with OpenLayers.Proto

المشرف العام

Administrator
طاقم الإدارة
I am completely new to JS and openlayers, i am trying to figure out a way to validate the received layer with the OpenLayers.Protocol.HTTP call. I want to validate that it is in the correct format (GeoJSON). I have tried callback function, and it doesnt seem to work. Also i have tried through the OpenLayers.Rquest.GET method but i cant really notify the user when the query has given an error. I can see that the GET method only validates if it has established a connection but not if the GeoJSON vector has the correct format.

In the test.php script called in the url param. The output in case of a successful query is a functional GeoJSON layer. what i am trying to achieve is when the query is not succesful,(has returned 0 rows).

Any of the alerts i have tried so far have been usefull.

function handler(request) { alert(request.responseText); alert(request.status); alert(request.getAllResponseHeaders()); var featurecollection = request.responseText; var geojson_format = new OpenLayers.Format.GeoJSON(); var geojson_vector = new OpenLayers.Layer.Vector(); geojson_vector.addFeatures(geojson_format.read(featurecollection)); alert(geojson_format.isValidType(request.responseText,"FeatureCollection")); alert(geojson_format.extract.collection); //~ alert(geojson_format.read(featurecollection)); if (geojson_format.read(request.responseText)) { alert("CORRECTO. SI SE PUDO CONECTAR A " + geojson_layer.protocol.options.url); } else{ alert("ERROR. NO SE PUDO CONECTAR A " + geojson_layer.protocol.options.url); } } //~ function test_exito() { //~ alert("CORRECTO. SI SE PUDO CONECTAR A " + geojson_layer.protocol.options.url); //~ } //~ //~ function test_fraca() { //~ alert("ERROR. NO SE PUDO CONECTAR A " + geojson_layer.protocol.options.url); //~ } function refresh(proy) { var request = OpenLayers.Request.GET({ url: "test.php?proyecto="+proy, format: new OpenLayers.Format.GeoJSON(), //~ success: test_exito, //~ failure: test_fraca, callback: handler }); if (geojson_layer) { //~ geojson_layer.name="Estado "+proy; geojson_layer.protocol.options.url="test.php?proyecto="+proy; geojson_layer.refresh({force:true}); } else { geojson_layer = new OpenLayers.Layer.Vector("Consulta", { styleMap: new OpenLayers.StyleMap({ "default": new OpenLayers.Style({ pointRadius: 15, fillColor: "#ffcc66", fillOpacity: 0.2, strokeColor: "#ffff00", strokeWidth: 2, strokeOpacity: 0.8 } ), "select": { fillColor: "#8aeeef", strokeColor: "#32a8a9", labelYOffset:13, label:"${name}"} //Text entspricht feature.attributes.name }), projection: new OpenLayers.Projection("EPSG:4326"), strategies: [new OpenLayers.Strategy.Fixed(), new OpenLayers.Strategy.Refresh({force:true,active:true})], protocol: new OpenLayers.Protocol.HTTP({ url: "test.php?proyecto="+proy, format: new OpenLayers.Format.GeoJSON() }) }); map.addLayer(geojson_layer); } }

أكثر...
 
أعلى