I've got a little problem with OpenLayers. I've a simple function which sets the center of the map to the position of the user - this works fine. But: it should also zoom - which, for an unknown reason, don't work.

Any ideas?

function gotSomeLocation(position){
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    map.setCenter(tCoord(long, lat));


1 Answer 1


use sth. like this:

map.setCenter(new OpenLayers.LonLat(long, lat),10);



map.setCenter(lonlat, zoom);

you dont also need to use map.zoomTo(18);


setCenter: function(    lonlat,
    forceZoomChange )

Set the map center (and optionally, the zoom level).

lonlat  {<OpenLayers.LonLat>|Array} The new center location.  If provided as    
array, the first value is the x coordinate, and the 2nd value is the y coordinate.
zoom    {Integer} Optional zoom level.
dragging    {Boolean} Specifies whether or not to trigger movestart/end events
forceZoomChange {Boolean} Specifies whether or not to trigger zoom change events (needed on baseLayer change)

i hope it helps you...

  • 1
    thank you, this works for me. but shouldn't it also work with the two commands seperated?
    – ABLX
    Commented May 9, 2012 at 11:49

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.