How do I add baselayer with two different projections?

I have added overlayer with a different projection(EPSG:4326), but I need to add base layer with various projection

  1. Google Maps
  2. My vector layer with EPSG:4326 projection

I can see everything is fine with Google Maps but when I switch base layer to EPSG:4326 layer everything is white.

window.onload = function init(){
                var lon = 84;
                var lat = 28;
                var zoom = 8;
                var url = "";

                var mapControls = [
                    new OpenLayers.Control.Navigation(),
                    new OpenLayers.Control.PanZoomBar(),
                    new OpenLayers.Control.LayerSwitcher({'ascending':false}),                       
                    new OpenLayers.Control.ScaleLine(),
                    new OpenLayers.Control.MousePosition(),
                    new OpenLayers.Control.OverviewMap(),
                    new OpenLayers.Control.KeyboardDefaults()

                var map = new OpenLayers.Map(
                    numZoomLevels: 21,
                    projection: new OpenLayers.Projection("EPSG:900913"),
                    displayProjection: new OpenLayers.Projection("EPSG:4326"),
                    controls: mapControls 

                var dsb = new OpenLayers.Layer.WMS( 
                "Nepal Border",
                {layers: 'GisData:IR'}                        

                var gmap_gs = new OpenLayers.Layer.Google(
                "Google Streets",// the default 
                {'sphericalMercator': true}                 


                var dG = new OpenLayers.Layer.WMS( 
                "2G Down Sites",
                {layers: 'GisData:dG', transparent: "true",format: "image/png"},
                    isBaseLayer: false,
                    buffer: 0

                map.addLayers([dsb, dG,gmap_gs]);
                var proj = new OpenLayers.Projection("EPSG:4326");
                var point = new OpenLayers.LonLat(lon,lat);

                point.transform(proj, map.getProjectionObject());

                map.setCenter(point, zoom);


5 Answers 5


I've been looking into this recently, and this is one of the top search results, I figured i'd add some breadcrumbs to my research and experimentation.

Long and Short:

The 2.X line of Open Layers does not support this. The Map, it's properties such as center, resolution and extents are set by the INITIAL base layer of the map. Change to another base layer DOES NOT adjust these properties, so the outcome is unknown. Often, you get a center point/bounds that are not valid for the base layer you are changing to, so you see white tiles. But that is not ALWAYS the outcome (for example, switching from 4326 to 900913 will bring you to a point close to 0degN,0degE).

But, hope is not lost.

Near term:

Some patches were written against open layers that attempt to patch this for the map and many of the layers (see [1]). This comes from a bug filed over 6 (6!!!) years ago [2]. While you'd have to host your own version of OL and patch it by hand (the patch will not apply cleanly to the latest versions, so you have to manually do the edit), this will get you mostly where you want to be. Very nasty, and doesn't handle everything, but overall, gets someone who NEEDS multiple baselayers with different projections on their feet.

Long term:

This is exactly one of the things that OpenLayers 3.0 is hoping to address[3].

One example is the base layer/overlay dichotomy. It is often a challenge to know how and where to set resolutions, maxExtent, etc. for the map. Projection handling between map and layers and the interplay between projection and properties like maxExtent and center are complicated at best.

The Map object in 2.X relied too heavily on the base layer, and that is going to change going forward. So, if you can hold out until late 2013, there may be a pre-made solution for you to use.

[1] http://trac.osgeo.org/openlayers/attachment/ticket/1249/NEW.Reproject.patch
[2] http://trac.osgeo.org/openlayers/ticket/1249
[3] http://openlayers.org/blog/2012/11/14/why-are-we-building-openlayers-3/


there is something as your question here, How to reproject base layers in OpenLayers? and you get some information about transformation, you should look this, Creating Spherical Mercator Raster Images.

to summarize briefly from first post:

  1. OpenLayers can transform vector layers (like WFS).
  2. Raster Layers cannot be transformed by OpenLayers.
  3. Any good WMS should be able to do the reprojection for you.
  4. ArcGIS Server can't provide layers in 900913 because that wkid is unsupported. 3857, on the other hand...!

and from second link:

One of the reasons that the Spherical Mercator projection is so important is that it is the only projection which will allow for overlaying image data on top of commercial layers like Google Maps correctly. When using raster images, in the browser, it is not possible to reproject the images in the same way it might be in a ‘thick’ GIS client. Instead, all images must be in the same projection. How to create Spherical Mercator projected tiles depends on the software you are using to generate your images.

The important thing is here:

using raster images, in the browser, it is not possible to reproject the images
in the same way it might be in a ‘thick’ GIS client.

and for transforming dataset, my answer can help you about this, here.

i hope it helps you...

  • I have added the code with proj4js which works for overlayes but not for 2 different base layers any comment on this?
    – kinkajou
    May 3, 2012 at 7:58

Set up your WMS layer with out any reference to 900913 (assuming your data is in 4326). Then when you add the layer to OpenLayers make sure that you have set the projection of the map to 900913 (or just make sure to add Google Maps first). Then when you add the WMS layer, OpenLayers will request the data in the map projection (900913) and GeoServer will reproject from 4326 to 900913 on the fly.

  • Added the code. There is problem when switching between these layer. When I load the layer with WGS 4326 first as base layer and maintain the zoom level it's ok after I switch to google layer and pan a map a little it's all gone.
    – kinkajou
    May 3, 2012 at 7:57
  • Change map.addLayers([dsb, dG,gmap_gs]); to map.addLayers([gmap_gs, dsb, dG]);
    – Ian Turton
    May 3, 2012 at 8:12
  • shows google map on first load but still can't switch layer. Error while switching
    – kinkajou
    May 4, 2012 at 5:34

In general this isn't possible as OpenLayers sets the projection of the map from the base layer.

One possible work around is to use a cascading WMS like GeoServer to reproject one of the base layers from one to the other projection.


I just applied the patch to ol 2.13.1 (from tag)

The last patch file from trac didn't work when no restrictedExtent was given, so I fixed it as well.

You can get it here, along with a text example:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.