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?


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.

  • Very useful! Can you show how to create a circle ? – Magno C Dec 6 '12 at 16:43
  • 1
    I edited the answer to show how to make a circle. I hope that helps. – katahdin Dec 6 '12 at 17:02
  • @Michiel Mark the answer and answered if you think he answered your question. – CaptDragon Dec 6 '12 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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