3

I'm trying to use GeoServer WFS JSON for creating a vector layer (instead of using WMS) because this is a layer I redraw every 20 seconds.

This is the code I use :

function getJSON() {

    var geoJsonUrl = "http://localhost:8080/geoserver/test/ows?service=WFS&version=1.1.0&request=GetFeature&typeName=test:Coords&srsName=EPSG:4326&format=text/javascript&outputFormat=json&format_options=callback:loadGeoJson";

    $j.ajax({
        url: geoJsonUrl,
        dataType: 'jsonp'
    });     
}

function loadGeoJson(data) {
    console.log('success');
}

The JSON from the request is :

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "id": "DIView.fid-25487b92_142a9c99a7e_-5722",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    34.8923,
                    32.1893
                ]
            },
            "geometry_name": "DeviceLocation",
            "properties": {
                "Id": 1,
                "DeviceSerial": "046009117212",
                "DateTime": "2013-12-03T12:20:48.080Z",
                "Spd": "4.72175   ",
                "Brn": "143.3     ",
                "Alt": "91.6      ",
                "DeviceName": "12",
                "DeviceReversedName": "21",
                "DeviceBattery": 3.71
            }
        },
        {
            "type": "Feature",
            "id": "DIView.fid-25487b92_142a9c99a7e_-5721",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    34.8025,
                    31.2813
                ]
            },
            "geometry_name": "DeviceLocation",
            "properties": {
                "Id": 2,
                "DeviceSerial": "046009117224",
                "DateTime": "2013-12-03T12:41:12.313Z",
                "Spd": "0         ",
                "Brn": "136       ",
                "Alt": "349       ",
                "DeviceName": "24",
                "DeviceReversedName": "42",
                "DeviceBattery": 0.29
            }
        },
        {
            "type": "Feature",
            "id": "DIView.fid-25487b92_142a9c99a7e_-5720",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    34.8026,
                    31.2812
                ]
            },
            "geometry_name": "DeviceLocation",
            "properties": {
                "Id": 3,
                "DeviceSerial": "047180050069",
                "DateTime": "2013-12-03T12:41:14.140Z",
                "Spd": "0.690988  ",
                "Brn": "161.8     ",
                "Alt": "340.2     ",
                "DeviceName": "4",
                "DeviceReversedName": "4",
                "DeviceBattery": 3.65
            }
        }
    ],
    "crs": {
        "type": "EPSG",
        "properties": {
            "code": "4326"
        }
    }
}

and I get error in Firebug :

enter image description here

Any idea ?!

6
  • Just curious: was the JSON from the request effectively JSON or a JavaScript object instead?
    – Decio
    Dec 9, 2013 at 16:16
  • it was JSON from Geoserver GetFeature.
    – Alophind
    Dec 9, 2013 at 20:42
  • It might not be related, but I encountered situations (not with geoserver though) where, depending on the request headers and the server settings, the parsed response is either JSON (effectively a string) or a JavaScript object. Now, who knows if your code is expecting one or the other... try something like: JSON.stringify(response) if the response is an obj and your code expects a string... or JSON.parse(response) for the opposite case... it might help to understand what is happening... if still relevant.
    – Decio
    Dec 9, 2013 at 21:00
  • I've managed to solve the problem by using Vector layer with : format: new OpenLayers.Format.GeoJSON
    – Alophind
    Dec 10, 2013 at 4:41
  • 1
    I posted an answer to the same problem here.
    – ustroetz
    Mar 11, 2014 at 10:26

0

Browse other questions tagged or ask your own question.