[GIS] OpenLayers zoom to Boundingbox depends on lat/lon positions

openlayers-2

I'll try to explain my application and the problem.

My application gets a position for a zip code and receives a Lat/Long position, then I'm looking for all points around this position and show them.(This works fine)

Now I want to try to show a user his zipcode position at the center of the map, but I also want to specify the correct zoom level, so that the user can see all points around this point. (it's not limited by the distance)

Is this possible?

My minLat,maxLat,minLng,maxLng looks like this :

Array ( [minLng] => 8.381 [maxLng] => 9.15259 [minLat] => 49.5103735 [maxLat] => 50.06762 )

Edit:

-HTML:

map.setCenter(new OpenLayers.LonLat(center.lon,center.lat) // Center of the map
            .transform(
                new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
                ), config.zoom // Zoom level
            );
    map.zoomToExtent(new OpenLayers.Bounds(config.bounds.minLng,config.bounds.minLat,config.bounds.maxLng,config.bounds.maxLat).transform(epsg4326));

I have added the html of the function.

Best Answer

You can use openlayers bounds to set extent and then zoom your map to extent using map.zoomToExtent() or map.zoomToMaxExtent()

Here's an example:

map.zoomToExtent(new OpenLayers.Bounds(minLng,minLat,maxLng,maxLat).transform("EPSG:4326", "EPSG:900913"))