1

I'm working with MapServer/PostGIS using OpenLayers to display map. I'm trying setCenter map but it is not working!

My code:

var bounds = new OpenLayers.Bounds(107.567139, 12.326586, 108.258591,
        12.890796);
var options = {
    controls : [],
    maxExtent : bounds,
    maxResolution : 0.0010217594533836,
    projection : "EPSG:4326",
    units : 'degrees',
    allOverlays : true
};

map = new OpenLayers.Map('map', options);
.....
//Declare some layers wms
.....
map.addLayer(wms_RanhGioi);
map.addLayer(wms_GiaoThong);
map.addLayer(wms_ATM);
map.addLayer(wms_Cafe);
map.addLayer(wms_KhachSan);
map.addLayer(wms_NhaHang);
map.addLayer(wms_TramXang);

map.addControl(new OpenLayers.Control.LayerSwitcher({
    'ascending' : false
}));
map.addControl(new OpenLayers.Control.PanZoomBar({
    position : new OpenLayers.Pixel(2, 15)
}));
map.addControl(new OpenLayers.Control.Navigation());
map.addControl(new OpenLayers.Control.Scale($('scale')));
map.addControl(new OpenLayers.Control.MousePosition({
    element : $('location')
}));
map.zoomToExtent(bounds);
map.setCenter(new OpenLayers.LonLat(12.67812, 108.04401), 5);

Can somebody help me?

3 Answers 3

2

You just put the center coordinates in wrong order. Longitude should come first and then the latitude. In your code, the point is out of the maxExtent you set in map options.

1

I have an extra (). Like this:

map.setCenter((new OpenLayers.LonLat(160000,485000)), 2);
0

Could be a problem with the map coordinate

var center = new OpenLayers.LonLat(12.67812, 108.04401);
center.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
map.setCenter(center,5);
2
  • Thanks for reply! But it's not working! You can give me a demo of setCenter in OpenLayers!
    – Si Do Dinh
    Commented Jan 29, 2013 at 11:55
  • 1
    Thanks to everyone! I sloved problem! It's map.setCenter(new OpenLayers.LonLat(108.04401, 12.67812), 5); Instead map.setCenter(new OpenLayers.LonLat(12.67812, 108.04401), 5);
    – Si Do Dinh
    Commented Jan 29, 2013 at 12:17

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.