3

I added a point like this and it works fine, but for polygon it's not working.

var iconFeature = new ol.Feature({
          geometry: new ol.geom.Point([8637791.36, 1456487.82]),
          name: 'Null Island',
          population: 4000,
          rainfall: 500
        });
var vectorSource = new ol.source.Vector({
  features: [iconFeature]
});

var vectorLayer = new ol.layer.Vector({
  source: vectorSource
});

My code for polygon is as follows -

var poly = new ol.Feature({
        geometry: new ol.geom.Polygon([8623931.28, 1449016.75],[8624007.72, 1458265.63],
                    [8629358.31, 1458571.37],[8628441.06, 1455284.58],[8625765.77, 1449781.12],[8630275.55, 1453450.09],
                    [8629281.88, 1452456.42],[8627294.51, 1451080.54],[8625765.76,1449781.11],[8623931.28, 1449016.75]),    
        name: 'hello',
        population: 200,
        rainfall: 40
        }); 
var vectorSource1 = new ol.source.Vector({
  features: [poly]
});

var vectorLayer1 = new ol.layer.Vector({
  source: vectorSource1
});

map.addLayer(vectorLayer,vectorLayer1); 

It's not adding polygon to map. Also on click I want a popup showing name, population and rainfall

1
  • The second part of your question already has answers and examples. This simple example uses the pointermove event and only gets the name property from the feature but would also work for click events and you could get other property values and append them and some formatting html to the text for the popup's innerhtml. gis.stackexchange.com/questions/223649/…
    – Mike
    Oct 8, 2018 at 13:21

1 Answer 1

6

Polygon geometry needs two additional pairs of brackets

    geometry: new ol.geom.Polygon([[[8623931.28, 1449016.75],[8624007.72, 1458265.63],
                [8629358.31, 1458571.37],[8628441.06, 1455284.58],[8625765.77, 1449781.12],[8630275.55, 1453450.09],
                [8629281.88, 1452456.42],[8627294.51, 1451080.54],[8625765.76,1449781.11],[8623931.28, 1449016.75]]]),    
1
  • thanks a lot, I will try this, but for mean time i m doing with other way. Thanks again.
    – Vishal
    Oct 8, 2018 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.