[GIS] Adding features/points/polygons to OpenLayers map

openlayers-2

I'm trying to get a bunch of markers/points/polygons to show up on my OpenLayers map, but nothing seems to show up and the vector_layer seems to only have two SVG attributes when it should have way more than that, so maybe I'm not creating the features correctly?

var vector_layer = new OpenLayers.Layer.Vector('Points', {
    styleMap: new OpenLayers.StyleMap({'default':{
        pointRadius: 6,
        labelYOffset: -15,
        labelOutlineColor: "white",
        labelOutlineWidth: 3,
        label: "BOOB",
        strokeColor: "#FF00FF",
        strokeOpacity: 1.0,
        strokeWidth: 6
    }})
});

polygonFeature = [];
polygonGeom = OpenLayers.Geometry.fromWKT('LINESTRING(588508.747252596 168674.954513992,588706.355881724 168104.100450511,588805.174332032 167818.655647441,588904.002207028 167533.198996286)');
polygonFeature.push( [ OpenLayers.Feature.Vector(polygonGeom, {
        label: 'Vallée de l\'impossible',
        color: '#00FF66',
        type: '5',
        strokeColor: '#FF9966',
        strokeWidth: 10
    }
)]);
polygonGeom = OpenLayers.Geometry.fromWKT('LINESTRING(588591.788935473 168704.056645848,588799.712933324 168593.991578248,589007.644549979 168483.931724448)');
polygonFeature.push( [ OpenLayers.Feature.Vector(polygonGeom, {
        label: 'Trainerlift',
        color: '#00FF66',
        type: '5',
        strokeColor: '#FF9966',
        strokeWidth: 10
    }
)]);
polygonGeom = OpenLayers.Geometry.fromWKT('LINESTRING(589367.815099211 167613.858604876,589342.892993503 167453.459513931,589317.969563007 167293.056263815,589293.04480767 167132.648854482,589268.118727426 166972.237285751,589243.191322243 166811.821557544,589218.262592051 166651.401669799,589193.332536805 166490.977622361,589168.401156456 166330.54941513)');
polygonFeature.push( [ OpenLayers.Feature.Vector(polygonGeom, {
        label: '',
        color: '#00FF66',
        type: '5',
        strokeColor: '#FF9966',
        strokeWidth: 10
    }
)]);
polygonGeom = OpenLayers.Geometry.fromWKT('LINESTRING(589381.008650411 167618.595027528,589528.167834526 167597.665516203,589675.32803885 167576.739543001,589822.489263299 167555.81710792,589969.651507792 167534.898210995,590116.814772244 167513.982852311,590190.396786934 167503.526499788,590263.979056575 167493.071031836,590411.144360695 167472.162749675,590484.727395153 167461.709935416,590558.310684527 167451.258005792)');
polygonFeature.push( [ OpenLayers.Feature.Vector(polygonGeom, {
        label: 'Kaiseregg',
        color: '#00FF66',
        type: '5',
        strokeColor: '#FF9966',
        strokeWidth: 10
    }
)]);
polygonGeom = OpenLayers.Geometry.fromWKT('LINESTRING(588032.051690777 168677.694251669,587854.411858591 168531.960867246,587727.027545819 168473.087930543,587604.551815243 168406.840665774,587597.086384338 168370.382898673,587586.346776547 168338.842624253,587572.267213375 168280.657156328,587617.379141907 168257.594442098,587674.670785861 168168.20828954,587692.528735376 168068.210711088,587678.42167755 167996.516748919,587671.368022863 167960.669460802,587664.314284324 167924.821967987,587647.392407747 167882.037079818,587614.905936395 167857.292284218,587579.166995959 167848.10676651,587531.979824223 167861.864699831,587488.295691056 167881.805865479,587448.741633028 167913.198156398,587407.948520243 167940.909752369,587367.155783926 167968.621499271,587352.395598283 168002.34037867,587387.659707869 168005.667732583,587471.24959004 167951.596495736,587523.332360333 167935.589860972,587588.546726326 167916.282148895,587595.567211413 167960.59637938,587598.66600232 167982.344965964,587605.028849588 167994.264361834,587630.924627957 168033.627737266,587665.855685172 168074.609313393,587656.466602515 168150.18764367,587608.510272684 168237.303976917,587546.823442271 168285.416528806,587556.537111271 168317.010308028,587566.250678296 168348.603937425,587585.677506407 168411.790746861,587709.592724068 168479.2623002,587833.505151093 168546.735653008,587874.498771181 168568.238839737,587915.535212451 168611.021736516,588014.954848258 168698.700956018,588030.412917213 168679.334411072)');
polygonFeature.push( [ OpenLayers.Feature.Vector(polygonGeom, {
        label: '',
        color: '#00FF66',
        type: '2',
        strokeColor: '#FF9966',
        strokeWidth: 10
    }
)]);
polygonFeature.push( [ OpenLayers.Feature.Vector(polygonGeom, {
        label: 'Schwyberg Schneeschuhtour',
        color: '#00FF66',
        type: '10',
        strokeColor: '#FF9966',
        strokeWidth: 10
    }
)]);

vector_layer.addFeatures(polygonFeature);

map.addLayer(vector_layer);

Here's a jsfiddle, it was based on another fiddle that works for OpenLayers, I just added my code above to it, might not work as I guess it needs to be in the same area/region?

http://jsfiddle.net/fAqae/

Best Answer

First, in your .push to polygonFeature you should not be pushing an array, so delete the array wrapping of the Vector. Second, you need a "new" in front of OpenLayers.Feature.Vector.

Here is the corrected code: http://jsfiddle.net/fAqae/2/

Related Question