0

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); 
1
  • Do you mean a single POI, or the extent of the POI layer? Commented Jun 17, 2014 at 0:25

1 Answer 1

1

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); 
2
  • Stephen, It does not work for me. The getExtent()-function returns NULL. I used another solution, which does not do exactly the same, but is also suitable for me: map.zoomToExtent(poiLayer.getDataExtent()); Commented Jun 23, 2014 at 21:28
  • @DirkVansina I'm not sure why your dataset would return null for getExtent() - but I'm glad you found a workaround using getDataExtent(). Since that also returns a Bounds object the map.setCenter approach may still work Commented Jun 24, 2014 at 2:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.