openlayers-3

OpenLayers 3.2.1 - Change default projection


I am having a difficult time trying to change the default projection of the map view.

Here is a JSFIDDLE: http://jsfiddle.net/qtcpjape/3/

$( document ).ready(function() {
    var proj = new ol.proj.Projection({
        code: 'EPSG:4326',
        units: 'm',
        extent: [-180.0000, -90.0000, 180.0000, 90.0000]
    });

    var map = new ol.Map({
        target: 'map',
        controls: [],
        layers: [
            new ol.layer.Tile({
                source: new ol.source.MapQuest({
                    layer: 'osm'
                })
            })
        ],
        view: new ol.View({
            center: [-90.78885827595732, 38.78662388327825],
            projection: proj,
            zoom: 15
        })
    });
});

I will be having GeoJSON coming from a REST call that is in EPSG:4326 and I would like to map it easily.

Right now, I cannot seem to get the map to change the projection and show the actual center.

Any help would be appreciated! Thanks!


Solution

  • As you are using MapQuest tiles, your map has to be in EPSG:3857 because ol3 does not support raster reprojection (raster reprojection is supported since version 3.11.0 but for performance reasons it is usually recommend to reproject the vector data). But your GeoJSON vector data can be reprojected on-the-fly from EPSG:4326 to EPSG:3857 by setting the target projection on the vector source.

    For example:

    var vectorSource = new ol.source.GeoJSON({
      ...
      projection: 'EPSG:3857'
    });
    
    
    var vectorLayer = new ol.layer.Vector({
      source: vectorSource
    });
    
    
    var map = new ol.Map({
        target: 'map',
        controls: [],
        layers: [
            new ol.layer.Tile({
                source: new ol.source.MapQuest({
                    layer: 'osm'
                })
            }),
            vectorLayer
        ],
        view: new ol.View({
            center: ol.proj.transform([6, 49], 'EPSG:4326', 'EPSG:3857'),
            zoom: 3,
            projection: 'EPSG:3857'
        })
    });
    

    See http://jsfiddle.net/qtcpjape/5/