1

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?

1 Answer 1

3

the best way is that:

map.zoomToExtent(vectorLayer.getDataExtent());

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

var totalBounds = firstExtent.extend(secondExtent);

map.zoomToExtent(totalBounds);
3
  • 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, 2013 at 14:14
  • i think you can extend your first bounds (first layer) with second bounds (second layer). i have updated my answer.
    – urcm
    Feb 14, 2013 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, 2013 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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