[GIS] OpenLayer2 – center map based on lon/lat of POI

openlayers-2

Is it possible to center the map to the coordinates of a POI, imported by using a text file? (OpenLayers.Layer.Text)

var pois = new OpenLayers.Layer.Text( "My Points",
                { location:"./textfile.txt",
                  projection: toProjection
                });
map.addLayer(pois);

map.setCenter (????, zoom);

Best Answer

Assuming that you mean to set the extent to the whole POI layer, then each layer has a function getExtent:

getExtent: function()

Returns {OpenLayers.Bounds} A Bounds object which represents the lon/lat bounds of the current viewPort.

You can use getCenterLonLat to find the centre point of the bounds, and feed this to the map.setCenter function. Try something like this (not actually tested):

map.addLayer(pois);
var poiBounds = pois.getExtent();
var lonLat = poiBounds.getCenterLonLat()
map.setCenter (lonlat, zoom);