TTT
TTT

Reputation: 1885

Adding a circle to a vector source in Open Layers 3

My code goes like this:

var circle = new ol.style.Circle({
                radius: 5,
                fill: null,
                stroke: new ol.style.Stroke({
                    color: 'rgba(255,0,0,0.9)',
                    width: 3
                })
            });
var circleFeature = new ol.Feature(circle);

I have tried

circle.setCoordinates([x,y]);

and

circleFeature.setCoordinates([x,y]);

But each time I get

Object doesn't support property or method 'setCoordinates'.

I guess I am not applying setCoordinates to the right object. The example code from or own application that I need to replicate with a Circl simply has a LineString instead of a Circle, but I don't find how to use it with a Circle so far.

Upvotes: 0

Views: 2005

Answers (1)

Jonatas Walker
Jonatas Walker

Reputation: 14168

It should be:

var circle = new ol.style.Style({
    image: new ol.style.Circle({
        radius: 5,
        fill: null,
        stroke: new ol.style.Stroke({
            color: 'rgba(255,0,0,0.9)',
            width: 3
        })
    })
});

var feature = new ol.Feature(
    new ol.geom.Point([0, 0])
);
feature.setStyle(circle);
vectorSource.addFeature(feature);

So, you apply a style to the feature and if you want to setCoordinates you can use:

feature.getGeometry().setCoordinates(coordinate);

A Fiddle demo.

Upvotes: 2

Related Questions