3

My map projection is EPSG:3857. And I want to show a GeoJSON data which has EPSG:4326 projection.

            var source = new ol.source.Vector({
                url: 'http://localhost:15485/api/locations',
                format: new ol.format.GeoJSON()
            });                
            var locationsGeojsonLayer = new ol.layer.Vector({
                source: source,
                projection: 'EPSG:3857'
            });

Locations are getting from url but does not apper on map. Openlayers does not automatically convert URL data.

if I use ajax call and add features manually it appears on map.

            $http({method: "GET", url: "http://localhost:15485/api/locations"}).then(function (response) {
                angular.forEach(response.data, function (item) {

                    var feature = (new ol.format.GeoJSON()).readFeature(item,{
                        dataProjection: 'EPSG:4326',
                        featureProjection: 'EPSG:3857'
                    });

                    source.addFeature(feature);
                })
            });
  • 1
    It would help to clarify what exactly the question is here, but I believe you have highlighted the primary issue here and that's the fact that ol.source.Vector() won't automatically reproject the data for you. You need to use the old.format.GeoJSON().readFeature() to project and display, as you have demonstrated. – the_skua Jul 26 '16 at 18:32

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.