[OpenLayers-Users] setCenter not working with EPSG:900913projection

Heidt, Christopher M. CHRISTOPHER.M.HEIDT at saic.com
Mon Jan 5 09:52:08 EST 2009


you need to transform the values you are applying to the setcenter.
Map.setCenter( new OpenLayers.LonLat(lon,
lat).transform(map.displayProjection, map.projection),null);

You may want to add {displayProjection: new
OpenLayers.Projection("EPSG:4326")} to your map options to make your
code a bit cleaner.

Whenever you pull data from the map you need to transform it from
projection to displayProjection
and when you add data to the map you need to transform from
displayProjection to projection.


-----Original Message-----
From: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org]
On Behalf Of Christopher Schmidt
Sent: Sunday, January 04, 2009 8:01 PM
To: Robert Connolly
Cc: users at openlayers.org
Subject: Re: [OpenLayers-Users] setCenter not working with
EPSG:900913projection

On Mon, Jan 05, 2009 at 09:37:38AM +1300, Robert Connolly wrote:
> 
> Hi Everyone,
> 
> I sent the following message before Christmas via Nabble, however I am

> unable to track down the reply I received. Can anyone help with my 
> problem?
> 
> Regards,
> 
> Rob Connolly
> 
> --Original Message--
> 
> Hi,
> 
> I'm having trouble using the setCenter method when using a EPSG:900913

> projection for a Google map layer, see my code below:
> 
> map = new OpenLayers.Map('map', {projection: new 
> OpenLayers.Projection("EPSG:900913")});
> var gmap = new OpenLayers.Layer.Google("Google Streets", { 
> sphericalMercator: true }); // streets is the default
> 
> map.addLayers([gmap]);
> 
> 
> var center = new OpenLayers.LonLat(-41.27, 173.28);

                              ^^^^^^

Lon,Lat is X,Y: you want 173.28, -41.27.

-- Chris

> alert('['+center.lon+','+center.lat+']');
> var proj = new OpenLayers.Projection("EPSG:4326");
> center.transform(proj, map.getProjectionObject()); 
> alert('['+center.lon+','+center.lat+']');
> map.setCenter(center, 13);
> 
> I have the proj4js library installed and included on the page. It 
> appears that the conversion is working (from the data being shown by 
> my alert statements), however the center of the map remains on the 
> equator/prime meridian intersection, when it should be over Nelson, 
> New Zealand!
> 
> Can anyone help? 
> 
> Regards,
> 
> Rob Connolly
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users

--
Christopher Schmidt
MetaCarta
_______________________________________________
Users mailing list
Users at openlayers.org
http://openlayers.org/mailman/listinfo/users



More information about the Users mailing list