I understand OSM can initially only work with EPSG: 3857, but the data in geojson is stored in EPSG: 4326.

var geojsonObject = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [[40, 52.70], [116.20, 70]] } } ] }; var vectorSource = new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(geojsonObject) }); var vectorLayer = new ol.layer.Vector({ source: vectorSource }); var rasterLayer = new ol.layer.Tile({ source: new ol.source.OSM() }); var map = new ol.Map({ layers: [rasterLayer, vectorLayer], target: document.getElementById('map'), view: new ol.View({ projection: 'EPSG:3857', center: [37.61, 55.75], zoom: 4 }) }); 

In this case, the map is displayed normally, but the line is drawn in the coordinates [0, 0] EPSG: 3857

If you change the projection on the EPSG: 4326

 var map = new ol.Map({ layers: [rasterLayer, vectorLayer], target: document.getElementById('map'), view: new ol.View({ projection: 'EPSG:4326', center: [37.61, 55.75], zoom: 4 }) }); 

the map starts to lag terribly, load for a long time and the upper part is stretched, but the line is drawn correctly EPSG: 4326

Tell me how to work with geojson in 4326
PS I saw a lot of posts about using ol.source.GeoJSON , but I get the error ol.source.GeoJSON is not a constructor

    1 answer 1

    No ol.source.GeoJSON - see the documentation.

    I can offer several options for working with geometry in a projection different from the map:

    1. Reprojection of coordinates before creating objects: ol.proj.transform

    2. Reprojection before adding to the layer: geomFeature.transform

    3. Reprojection by adding to layer event: srcVector.on('addfeature'

    PS
    Learn to use the documentation;)

    Your example is enough to write this:

     var geojsonObject = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [[40, 52.70], [116.20, 70]] } } ] }; var vectorSource = new ol.source.Vector({ features: ( new ol.format.GeoJSON() ).readFeatures( geojsonObject ).map( function( feature ) { console.log( feature.getGeometry().getCoordinates()[0] ); feature.getGeometry().transform( 'EPSG:4326', 'EPSG:3857' ); console.log( feature.getGeometry().getCoordinates()[0] ); return feature; } ) }); 
     <script src="http://openlayers.org/en/v3.17.1/build/ol.js"></script> 

    Console output added for clarity.