Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to OpenLayers. In one experiment I am doing I want to load, incrementally, features in a vector layer. In my setting, these features should be loaded from distinct OSM files (generated dynamically) in a HTTP server.

I know how to create a vector layer from a single OSM file, I have done it with a code similar to this:

map = new OpenLayers.Map('map_element', {
    maxResolution: 156543.0399,
    numZoomLevels: 16,
    units: 'm', 
    projection: new OpenLayers.Projection('EPSG:900913'),
    displayProjection: new OpenLayers.Projection('EPSG:4326')

vectorLayer = new OpenLayers.Layer.Vector("POIs", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
    url: "my_dynamically_generated_osm_file.osm",
    format: new OpenLayers.Format.OSM()
    projection: new OpenLayers.Projection("EPSG:4326")

map.addLayers([<other_layers_here>, vectorLayer]);

However, I am wondering how I can create first the vector layer without features, and afterwards adding new features defined in different OSM files according to the requests of the user. I know vector layer objects have a method addFeatures(), so I just need to know how could I obtain these features from the distinct OSM files provided by my http server ?

share|improve this question
up vote 1 down vote accepted

For example:

//If projection of source data is different from map projection we have to describe this
var osm = new OpenLayers.Format.OSM({
    'internalProjection': new OpenLayers.Projection("EPSG:900913"),
    'externalProjection': new OpenLayers.Projection("EPSG:4326")

vector = new OpenLayers.Layer.Vector()

    url: "",
    success: function(e) {
        features =;
share|improve this answer
Thanks for your answer @drnextgis!, one question about the projections: In my example, when I instantiated the vector layer, I added in its properties:"projection: new OpenLayers.Projection("EPSG:4326")" Is this correct considering it is loading OSM data?, or should I had specified the same projection I declared for the map (EPSG:900913)? or rather should I had declared the osm format object as you did in your example (specifying internal and external projections)?. Thanks for any clarification, the projection details are breaking my head now. By default the OSM data is in EPSG:4326, isn't it? – Sergio Aug 2 '12 at 9:04
Yes, you are correct. Note that even if you set the projection object on a layer, if you are adding features to the layer manually (via layer.addFeatures), they must be transformed before adding to the layer. OpenLayers will only transform the projection of geometries that are created internally to the library, to prevent duplicating projection work. – drnextgis Aug 2 '12 at 9:13
I see, so the extra step of declaring the format object with internal and external projections was only necessary because we are adding layers manually. If I understood correctly, in my original code that step was not necessary since when setting the projection property of the layer (with "projection: new OpenLayers.Projection("EPSG:4326")"), I am explicitly saying that the expected features are in the "EPSG:4326" projection, then behind the curtains those features will be transformed to the projection of the map ("EPSG:900913"), correct? – Sergio Aug 2 '12 at 11:37
Yes, only for EPSG:4326 and EPSG:900913 and their equivalents. Otherwise you should use proj4js library – drnextgis Aug 3 '12 at 3:20

Your Answer


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.