Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm wondering what the best strategy to position a map that has two points, using OpenLayers is. At the moment, my approach is to calculate the mid-point of the two points, and to zoom the map to that mid-point using the following approach:

var centerPoint = new OpenLayers.LonLat(lon, lat);      
map.setCenter(centerPoint, zoomLevel); 

However, this is not ideal as I don't know what the zoomLevel should be, so sometimes although the map is centered, the points are not visible. Is there a better approach to dynamically position the map?

share|improve this question

the best way is that:


UPDATE for comment:

var firstExtent = firstLayer.getDataExtent();
var secondExtent = secondLayer.getDataExtent();

var totalBounds = firstExtent.extend(secondExtent);


i hope it helps you...

share|improve this answer
Do you know if there is a way of handling data on two separate layers? I have the two points style differently (I could also try to have them on the same layer but with separate styles I suppose). – djq Feb 14 '13 at 14:14
i think you can extend your first bounds (first layer) with second bounds (second layer). i have updated my answer. – Aragon Feb 14 '13 at 14:34
This does make sense, but strangely firstExtent.extend(secondExtent) is undefined (both firstExtent and secondExtent have bounds that exist). – djq Feb 14 '13 at 20:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.