Draw a circle with defined diameter in OpenLayers

寵の児 提交于 2019-12-08 01:14:25

问题


I'm trying to write a code which let my users define some points on the map and once they created a point, the program should draw a circle with defined diameter(in kilometers or ... ) around the point.

I can draw a point but I don't know how I could handle what I said.

Here is an example about what I want:


回答1:


Use the following function to create circular points around a point.

//pass the 
//@pointX, 
//@pointY, 
//@radius of circle (in your case diameter/2)
//@pointsToFind this is how detail you want the circle to be (360 if you want one point for each rad)

function createCirclePointCoords(circleCenterX,circleCenterY,circleRadius,pointsToFind){
      var angleToAdd = 360/pointsToFind;
      var coords = [];  
      var angle = 0;
        for (var i=0;i<pointsToFind;i++){
        angle = angle+angleToAdd;
            console.log(angle);
        var coordX = circleCenterX + circleRadius * Math.cos(angle*Math.PI/180);
        var coordY = circleCenterY + circleRadius * Math.sin(angle*Math.PI/180);
            coords.push([coordX,coordY]);
        }

    return coords;
    }

And then create a polygon out of the coordinates returned from the function

var circleCoords = createCirclePointCoords(0,0,1000,360);
var geom = new ol.geom.Polygon([
circleCoords
])


来源:https://stackoverflow.com/questions/33996409/draw-a-circle-with-defined-diameter-in-openlayers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!