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

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

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).

share|improve this answer
    
Yeah and you could also use with these two methods .getBounds().getCenterLonLat() –  CaptDragon Jan 11 '12 at 15:29
    
Worked great. The easiest way was to use .getDataExtent since all of my features were on the one layer. Thanks! –  Munzilla Jan 11 '12 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 Jan 11 '12 at 22:19
    
Agreed, however, it's pretty frustrating in the early stages. I think I'm slowly getting the hang of it. –  Munzilla Jan 12 '12 at 13:55
add comment

This is an example of what harry is talking about.

var bounds = new OpenLayers.Bounds();
for (var x in _layer.selectedFeatures) {
    bounds.extend(_layer.selectedFeatures[x].geometry.getBounds());
}
//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.
map.zoomToExtent(bounds,true);

tested... works great.

share|improve this answer
    
This is what I would do. +1 for the code, nice and simple, works a treat –  Hairy Jan 12 '12 at 7:31
    
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 Feb 25 '13 at 14:43
add comment

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.

share|improve this answer
    
How do you query the MBR? –  CaptDragon Jan 11 '12 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 Jan 11 '12 at 15:49
    
that sounds like a lot more work than just using the framework ;) –  canisrufus Jan 11 '12 at 15:55
    
You're right.. i added an answer with an example code.(+1) –  CaptDragon Jan 11 '12 at 16:03
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.