I have several polygons being drawn on a map, usually representing a country or province or state. Depending on some chosen data, different regions may be selected, so what I was hoping is that there's a way to determine the center point between all of the features on the layer so the map can automatically be focused on the most relevant point. any help would be great. Thanks

3 Answers 3


This is an example of what harry is talking about.

var bounds = new OpenLayers.Bounds();
for (var x in _layer.selectedFeatures) {
//var center = bounds.getCenterLonLat(); <-- you don't really need this if you want to zoom. But it will give you the center lat long coords.

tested... works great.

  • I recommend map.zoomToExtent(bounds,false); otherwise some features can be outside the extent. This is also mentioned in the documentation for the boolean argument: "Find the zoom level that most closely fits the specified scale. Note that this may result in a zoom that does not exactly contain the entire extent. Default is false."
    – steffan
    Commented Feb 25, 2013 at 14:43

It depends on what you mean by "center point". If you're trying to show all of the polygons from a center point you'd need two things: 1. the center point, and 2. the zoom level/scale that would show you all of the polygons.

One way to get that centerpiont would be to add the polygons to a geometry collection (http://dev.openlayers.org/releases/OpenLayers-2.8/doc/apidocs/files/OpenLayers/Geometry/Collection-js.html) and then use the collection's getCentroid function to get the centroid of the collection. That still leaves you needing to know how zoomed you should make the map, though, so it's probably not the most efficient solution.

Given that the polygons are all on the same vector layer, a better solution would be to use vector_layer.getDataExtent(). This returns a Bounds object, which you could then use to update the map extent: map.zoomToExtent(bounds).

  • Yeah and you could also use with these two methods .getBounds().getCenterLonLat()
    – CaptDragon
    Commented Jan 11, 2012 at 15:29
  • Worked great. The easiest way was to use .getDataExtent since all of my features were on the one layer. Thanks!
    – Munzilla
    Commented Jan 11, 2012 at 20:13
  • @Munzilla No problem! I highly suggest getting comfortable with the OpenLayers api documentation. It's a really well laid out framework once you know how to look for stuff.
    – canisrufus
    Commented Jan 11, 2012 at 22:19
  • Agreed, however, it's pretty frustrating in the early stages. I think I'm slowly getting the hang of it.
    – Munzilla
    Commented Jan 12, 2012 at 13:55

If you query the MBR (minimum bBounding Rectangle) of all of the polygons, you automatically have the centre point. So start there, on loading, check what polygonsd have been selected and either query the MBR of them all, or get the min/max x/y and work from there.

  • How do you query the MBR?
    – CaptDragon
    Commented Jan 11, 2012 at 15:46
  • The MBR is the bounds of all of the objects, but its very easy to find the min and max x/y of a polygon, so it's easy to find out the min max of all the min and max
    – Hairy
    Commented Jan 11, 2012 at 15:49
  • that sounds like a lot more work than just using the framework ;)
    – canisrufus
    Commented Jan 11, 2012 at 15:55
  • You're right.. i added an answer with an example code.(+1)
    – CaptDragon
    Commented Jan 11, 2012 at 16:03

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.