Tuesday, 14 January 2020

distance - How to draw a polygon with preset length in OpenLayers?


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?



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...