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
|
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). |
|||||||||
|
|
This is an example of what harry is talking about.
tested... works great. |
|||||
|
|
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. |
|||||||||
|
