0

In openlayers I try get geojson from geoserver to overlay in map. Code:

  var vectorSource = new ol.source.Vector({
    format: new ol.format.GeoJSON(),
})
$.ajax({
    type: "GET",
    url: "http://localhost:8080/geoserver/ows?service=WFS&version=2.0&request=GetFeature&typeName=global:ua46lo_hydran_poi&SRS=EPSG:4326&outputFormat=text/javascript&format_options=callback:getJson",
    dataType : 'jsonp',
    jsonpCallback : 'getJson',
    success:function(data){
        // If response is valid
        var geojsonFormat = new ol.format.GeoJSON();
        console.log(data);
        // reads and converts GeoJSon to Feature Object
        var features = geojsonFormat.readFeatures(data);
        vectorSource.addFeatures(features);
    }
});
var restaurantsold = new ol.layer.Vector({
    title: 'b_layer',
    source : vectorSource
});

Add this layer to map, but get only this: enter image description here

Why? geojson from geoserver:

    {
  "type": "FeatureCollection",
  "totalFeatures": 2450,
  "features": [
    {
      "type": "Feature",
      "id": "ua46lo_hydran_poi.1",
      "geometry": {
        "type": "Point",
        "coordinates": [
          24.068626,
          49.785831
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "gid": 1,
        "mp_type": "0x5000",
        "label": "Антонича Б. вул., 6",
        "indicator": "Так",
        "typnet": "К",
        "dsitky": "200",
        "typpg": "М"
      }
    },
    {
      "type": "Feature",
      "id": "ua46lo_hydran_poi.2",
      "geometry": {
        "type": "Point",
        "coordinates": [
          24.069041,
          49.785097
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "gid": 2,
        "mp_type": "0x5000",
        "label": "Антонича Б. вул., 12",
        "indicator": "Так",
        "typnet": "К",
        "dsitky": "600",
        "typpg": "М"
      }
    },
    {
      "type": "Feature",
      "id": "ua46lo_hydran_poi.3",
      "geometry": {
        "type": "Point",
        "coordinates": [
          24.068407,
          49.783316
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "gid": 3,
        "mp_type": "0x5000",
        "label": "Антонича Б. вул., 24",
        "indicator": "Так",
        "typnet": "К",
        "dsitky": "600",
        "typpg": "М"
      }
    },
    {
      "type": "Feature",
      "id": "ua46lo_hydran_poi.4",
      "geometry": {
        "type": "Point",
        "coordinates": [
          24.067433,
          49.783146
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "gid": 4,
        "mp_type": "0x5000",
        "label": "Антонича Б. вул., 26",
        "indicator": "Так",
        "typnet": "К",
        "dsitky": "250",
        "typpg": "М"
      }
    },
    {
      "type": "Feature",
      "id": "ua46lo_hydran_poi.5",
      "geometry": {
        "type": "Point",
        "coordinates": [
          24.060515,
          49.791894
        ]
      },
      "geometry_name": "geom",
      "properties": {
        "gid": 5,
        "mp_type": "0x5000",
        "label": "Антоненка-Давидовича Б. вул., 1",
        "indicator": "Так",
        "typnet": "К",
        "dsitky": "250",
        "typpg": "М"
      }
    }
  ],
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:EPSG::4326"
    }
  }
}
1

Your map is in Web Mercator projection (EPSG:3857) and your data in Lat/Lon (EPSG:4326) as all GeoJSON is (see the spec). Therefore your data ends up near Null Island (or 0,0).

The solution is simple transform your data to EPSG:3857 before adding it to the map.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.