Eddilbert Macharia
Eddilbert Macharia

Reputation: 186

openlayers 3 try to load geojson object

Im a bit new to workng with openlayers 3.

im trying to load a geojson object in openlayers 3 but it display the center only point[0,0], which not even in my geojson object, where im i going wrong.

the code is below:

//CREATE A BASE LAYER
        var raster = new ol.layer.Tile({
          source: new ol.source.OSM()
        }); 

        // create a source from the feature
        var source = new ol.source.GeoJSON(

            ({
                    object:
                        {
                            'type': 'FeatureCollection',
                            'crs': {
                                'type': 'name',
                                'properties': {'name': 'EPSG:3857'}
                            },
                            'features': [
                                {
                                    'type': 'Feature',
                                    //'properties': {'id': 63},
                                    'geometry': {
                                        'type': 'Point',
                                        'coordinates': [12, 12]
                                    }
                                },
                                {
                                    'type': 'Feature',
                                    'properties': {'id': 62},
                                    'geometry': {
                                        'type': 'Point',
                                        'coordinates': [35.0, 1.0]}
                                },
                                {
                                    'type': 'Feature',
                                    'properties': {'id': 61},
                                    'geometry': {
                                        'type': 'Point',
                                        'coordinates': [34.0, 0.0]}
                                },
                                {
                                    'type': 'Feature',
                                    'properties': {
                                        'id': 56
                                    },
                                    'geometry': {
                                        'type': 'Point',
                                        'coordinates': [33.0, 33.0]
                                    }
                                }
                            ]
                        }
            })
        );


       // CREATE THE LAYER WITH THE DATA
        var vectorLayer = new ol.layer.Vector({
            source: source
        });

       // create an openlayers map object
        var map = new ol.Map({
            target: attrs.id,
            layers: [raster,vectorLayer],
            view: new ol.View({
                //projection:'EPSG:4326',
                center:[0,0],
                zoom:2
            })
        });
    }

Upvotes: 0

Views: 1648

Answers (1)

Eddilbert Macharia
Eddilbert Macharia

Reputation: 186

i figured it out, i had to do a reprojection of my geojson object,

openlayers3 by default uses epsg:3857,so does my base map which is openstreetmap i.e projected coordinate system, so i had to turn my geojson to a projected system since it was coming in using the longitude and latitudes i.e. geographical coordinate system.(geojson is always espg:4326)

var source = new ol.source.GeoJSON({
                    object:{
                        'type': 'FeatureCollection',
                        'crs': {
                            'type': 'name',
                            'properties': {'name': 'EPSG:4326'}
                        },
                        'features': [
                            {
                                'type': 'Feature',
                                //'properties': {'id': 63},
                                'geometry': {
                                    'type': 'Point',
                                    'coordinates': [12, 12]
                                }
                            },
                            {
                                'type': 'Feature',
                                'properties': {'id': 62},
                                'geometry': {
                                    'type': 'Point',
                                    'coordinates': [35.0, 1.0]}
                            },
                            {
                                'type': 'Feature',
                                'properties': {'id': 61},
                                'geometry': {
                                    'type': 'Point',
                                    'coordinates': [34.0, 0.0]}
                            },
                            {
                                'type': 'Feature',
                                'properties': {
                                    'id': 56
                                },
                                'geometry': {
                                    'type': 'Point',
                                    'coordinates': [33.0, 33.0]
                                }
                            }
                        ]
                    },
                    projection: 'EPSG:3857'
            }
        );

Upvotes: 2

Related Questions