Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indicating polygon vertices with small circles #3165

Closed
ttsiodras opened this issue Jan 23, 2015 · 9 comments
Closed

Indicating polygon vertices with small circles #3165

ttsiodras opened this issue Jan 23, 2015 · 9 comments

Comments

@ttsiodras
Copy link

I am using OpenLayers 3 and have more or less implemented everything in my requirements list, except one thing: I am asked to somehow make the polygon rendering indicate the polygon's vertices with small circles.

This helps the users avoid creating accidental overlaps (or slivers) in the polygons they draw, when a neighboring polygon's edge appears to be a straight line, but in fact it's almost a straight line, and has a vertex right along its middle. Here's an example - look at the right edge of Polygon A below:

feature

In plain words, the desired polygon outline is not just a line - it is a line "adorned" with small circles in all the places where there is a vertex.

How can I do that in OL3? I searched in the ol.style.Style docs (that is, the style I pass via setStyle to the ol.layer.Vector containing my polygons), but didn't find anything relevant.

@tsauerwein
Copy link
Member

You could this by setting a geometry function returning a MultiPoint geometry with all vertices on the style. See also this example: http://openlayers.org/en/master/examples/earthquake-clusters.html

But please ask questions on the mailing list or StackOverflow.

@elemoine
Copy link
Member

@tsauerwein do you know how to display both the polygon and its vertices?

@tsauerwein
Copy link
Member

@elemoine You have two styles, one for the polygon and one for the points. And the style for the points has a custom geometry function:

var styleFunction = function() {
  var image = new ol.style.Circle({
    radius: 5,
    fill: null,
    stroke: new ol.style.Stroke({color: 'orange', width: 2})
  });
  return [
      new ol.style.Style({
          image: image,
          geometry: function(feature) {
              var coordinates = feature.getGeometry().getCoordinates()[0];
              return new ol.geom.MultiPoint(coordinates);
          }
      }),
      new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: 'blue',
          width: 3
        }),
        fill: new ol.style.Fill({
          color: 'rgba(0, 0, 255, 0.1)'
        })
      })
  ];
};

http://jsfiddle.net/gnegwsgx/1/

@tonio
Copy link
Member

tonio commented Jan 26, 2015

Neat, wouldn’t this deserve an example?

@elemoine
Copy link
Member

@tsauerwein, I see. Thanks for the answer.

@bartvde
Copy link
Member

bartvde commented Jan 27, 2015

agree with @tonio that this would make a nice example

@ttsiodras
Copy link
Author

Thank you for posting the answer to this - I tested the geometry function and it works perfectly.

As for asking on StackOverflow first, I did - and I just added your response there.

EDIT:

Apparently you did too, at the same time - one minute after me :-) I removed my own response, and accepted yours - thanks again.

@karthikbht91
Copy link

Great work, I think this is part of examples section in OpenLayers page. Could someone help me select these created circles. I need to allow certain functionality only to these circles on the corners of a Polygon..

@mohong
Copy link

mohong commented Oct 1, 2021

@elemoine You have two styles, one for the polygon and one for the points. And the style for the points has a custom geometry function:

var styleFunction = function() {
  var image = new ol.style.Circle({
    radius: 5,
    fill: null,
    stroke: new ol.style.Stroke({color: 'orange', width: 2})
  });
  return [
      new ol.style.Style({
          image: image,
          geometry: function(feature) {
              var coordinates = feature.getGeometry().getCoordinates()[0];
              return new ol.geom.MultiPoint(coordinates);
          }
      }),
      new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: 'blue',
          width: 3
        }),
        fill: new ol.style.Fill({
          color: 'rgba(0, 0, 255, 0.1)'
        })
      })
  ];
};

http://jsfiddle.net/gnegwsgx/1/

If I want to change the current click vertice`s style, like change it`s color, how can I do?

image

Because I want to remove this vertice by modify.removePoint(). (For better user experience).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants