I'm trying to change my map from using JSON to JSON-P for the source of data from my GeoServer. Unfortunately, what should be a vector layer covering the entire North Atlantic displays as a point in the Gulf of Guinea--so a classic projection error. But I can't see what I'm missing.
TL;DR: If I actually request the data from Geoserver using SRSNAME=EPSG:900913 I don't have a problem, but I can't figure out why I didn't need to do that originally!
The original vector layer is created by:
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: '//'+$(location).attr('host')+'/geoserver/SAHFOS/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=SAHFOS:CPRStdAreasIntRes&maxFeatures=50&outputFormat=application/json',
format: new ol.format.GeoJSON()
})
});
and works fine. The 41 features fit exactly to the coastlines of a world map. Trying to do it with JSON-P:
var stdAreas = new ol.layer.Vector({
// Source retrieving WFS data in GeoJSON format using JSONP technique
source: new ol.source.Vector({
loader: function(extent) {
$.ajax('http://domain/geoserver/SAHFOS/ows', {
type: 'GET',
data: {
service: 'WFS',
version: '1.0.0',
request: 'GetFeature',
typeName: 'SAHFOS:CPRStdAreasIntRes',
maxFeatures: '50',
outputFormat: 'text/javascript'
},
dataType: 'jsonp',
jsonpCallback:'callback:loadFeatures',
jsonp: 'format_options'
})
}
})
});
var loadFeatures = function(response) {
var source = stdAreas.getSource();
source.addFeatures(new ol.format.GeoJSON().readFeatures(response, {dataProjection: 'EPSG:4326'}));
var fmt=new ol.format.GeoJSON().readProjection(response)
};
fmt is EPSG:4326, so that dataProjection shouldn't be necessary, but nevertheless, the layer still ends up being a point in the Gulf of Guinea.
SRSNAME=EPSG:900913, I don't need to specify either Projection parameter.In any case, the question is why is it different between JSON and JSONP, not how to make it work--as you can see, I have got it working.
– Auspex Feb 09 '17 at 15:26