3

I am completely new to JavaScript 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 doesn't seem to work. Also I have tried through OpenLayers. Request.GET method but I can't 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 parameter, 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 successful, (has returned 0 rows).

None of the alerts I have tried so far have been useful.

        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);
        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 refresh(proy) {        
            var request = OpenLayers.Request.GET({
            url: "test.php?proyecto="+proy,
            format: new OpenLayers.Format.GeoJSON(),
            callback: handler
            });
            if (geojson_layer) {
            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);
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.