Mnementh
Mnementh

Reputation: 51331

How can I mix layers with different coordinate system in OpenLayers?

I'm using an OpenLayers-map and I want to use in it different mapservers, that use different coordinate systems. Can OpenLayers integrate it in the same map and automatically converts coordinate-systems?

Upvotes: 3

Views: 4920

Answers (3)

Andre Steenveld
Andre Steenveld

Reputation: 148

Depending on the layers, you will always have some sort of baselayer (the map) wich you can't really convert. If you want to add data (markers, geo json stuff, etc) on that map you will have to convert it to the projection the baselayer is using.

For markers this can easyly be done by:

/ defining our coordinate systems
var google = new OpenLayers.Projection("EPSG:900913"),
    latlon = new OpenLayers.Projection("EPSG:4326");

// transforming the location to the right coordinate system
var location = new OpenLayers.LonLat( 10, 10 ).transform( latlon, google );

// assuming you made an icon and marker layer
var marker = new OpenLayers.Marker( location, icon );     

markerLayer.addMarker( marker );

Check out the Openlayers documentation about transforming location from one system to another.

Upvotes: 3

cmy2k
cmy2k

Reputation: 21

This is an old question, but I came across this from a search and thought I might chime in in case this is helpful to others.

This may vary from the situation faced in the question posed, but I ran into a similar situation recently. In my case, I needed to show the output from two different WMS providers on one map. One provided their services in EPSG:900913, the other in EPSG:3857. Knowing that these are functionally equivalent, I figured if I could just request them in a way that made the services work, that the map would be able to work with the output. My map is in 900913 (and the other services therefore request using this projection by default).

How I was able get the other service to be requested correctly was as follows (with your information filled in):

var myLayer = new OpenLayers.Layer.WMS(
    "Name",
    "URL", {
        "layer": "layer"
    });

myLayer.projection = "EPSG:3857";

Usually appending ?request=getCapabilities to the service URL will allow you to see what projections are available from the service in the CRS tags of a desired layer.

Upvotes: 2

winwaed
winwaed

Reputation: 7829

If the map servers are providing different rasters then you may be out of luck.

However, if they are providing vectors (eg. KML files) or JavaScript-written map objects (eg. Dre's answer) then you can transform between different projections, so that all the data appears on the same projection and coordinate system as the base map. OpenLayers has the hooks for this (see Dre's answer) but you will probably have to include the Proj4JS library which provides the functionality.

Or you could use Proj4JS yourself to transform the coordinates before plotting.

Upvotes: 2

Related Questions