[GIS] How to draw a polygon with preset length in OpenLayers

distanceopenlayers-2polygon-creation

For a project I am working on, I need to be able to put a polygon on a map using OpenLayers, based on a variable point that can be set by a user. The polygon is an isosceles triangle of which two legs should be exactly 1 kilometer and should originate from a point set by the user under an angle of 90 degrees. Does anyone know how this can be done?

Best Answer

The standard way to add a regular polygon to an OpenLayers map is with the createRegularPolygon method.

Here's an example:

var lat = 40.12345
var lon = -90.54321
var center_point = new OpenLayers.Geometry.Point(lon, lat);
var triangle = new OpenLayers.Geometry.Polygon.createRegularPolygon(center_point, 1.2, 3, 90);

In this example the lon/lat values are in degrees (e.g., EPSG:4326), and the radius (1.2) is in those map units as well.

EDIT: If you want a circle instead of a triangle, change the "sides" parameter (3 in the example above) to 20, which gives a good approximation of a circle.

Related Question