Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
add comment

1 Answer

the best way is that:

map.zoomToExtent(vectorLayer.getDataExtent());

UPDATE for comment:

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

var totalBounds = firstExtent.extend(secondExtent);

map.zoomToExtent(totalBounds);

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
add comment

Your Answer

 
discard

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.