How to draw horizontal lines in openLayers?

I use openlayers and can draw lines using DrawFeature and OpenLayers.Handler.Path. Now I need to provide the ability to draw horizontal / vertical lines. This means that when the user starts to draw a line and moves the mouse at an angle, if the angle is less than 45 degrees along the x axis, he will only draw horizontally, and if the angle is more than 45 degrees, he will begin to draw vertical lines.

Any ideas on how to do this?

+6
source share
1 answer

This is a difficult question. I think you should somehow combine several controls

I would try to draw two hidden vectors the moment you want to start drawing. One vector for the horizontal axis and one for the vertical. Of course, they intersect at the starting point.

The actual line you want to draw must be snapped to one of these vectors. http://dev.openlayers.org/docs/files/OpenLayers/Control/Snapping-js.html

You can take a look at binding examples: http://www.openlayers.org/dev/examples/snap-grid.html , http://www.openlayers.org/dev/examples/wfs-snap-split.html

The only difference is that you can only draw a β€œgrid” if you know the source. I would start with an example that assumes you are doing this.

+1
source

Source: https://habr.com/ru/post/902048/


All Articles