OpenLayers中如何画扇形

项目中用到了地图展示,但是有个需求是根据参数画扇形,但是OpenLayers没有画扇形的现成函数,在某个牛人的帮助下,修改了下源代码便可以了

1,首先在源码中加入以下代码,最好是在OpenLayers.Geometry.Polygon.createRegularPolygon这个函数的源代码后面加入:

//参数分别是圆心,半径,边数,弧度,旋转角度(即右边半径与x正向轴的角度)
OpenLayers.Geometry.Polygon.createRegularPolygonCurve = function(origin, radius, sides,r,angel) {
	var rotation = 360 - r;
	var angle = Math.PI * ((1/sides) - (1/2));
	if(rotation) {
		angle += (rotation / 180) * Math.PI;
	}    
	var rotatedAngle, x, y;
	var points = [];
	for(var i=0; i<sides; ++i) {
		var an = i*((360 - rotation)/360);
	rotatedAngle = angle + (an * 2 * Math.PI / sides);
	x = origin.x + (radius * Math.cos(rotatedAngle));
	y = origin.y + (radius * Math.sin(rotatedAngle));
	points.push(new OpenLayers.Geometry.Point(x, y));
	}
	if(rotation!=0){
		points.push(origin);
	}
	var ring = new OpenLayers.Geometry.LinearRing(points);
	ring.rotate(parseFloat(r)+90+parseFloat(angel),origin);
	return new OpenLayers.Geometry.Polygon([ring]);};

2,客户端使用例子:

var map=new OpenLayers.Map("map_canvas");//初始化地图对象
vector_site_layer = new OpenLayers.Layer.Vector('Vector Layer');//生成一个图层层,放所有扇形
map.addLayers([vector_site_layer]);//将图层加入地图
var origi_point = new OpenLayers.Geometry.Point('100', '40');//根据经纬度生成圆点
//调用画扇形的函数
var sector = new OpenLayers.Geometry.Polygon.createRegularPolygonCurve(
					origi_point,//圆心
					gain,//半径
					100,//边数
					antBearing,//弧度
					lobeAngle //方向角
					);				

			//通过扇形生成一个Feature,并加入了各种自定义属性
var polygonFeature = new OpenLayers.Feature.Vector( 
					sector,
					{
						'id':'sector_',//扇形id
						'type':'sector',//类型
						'name':'join'//名字
					});
vector_site_layer.addFeatures(polygonFeature);//将扇形加入图层

大功告成,当然还可以根据扇形的参数不同显示不同的颜色。

转载自:https://blog.csdn.net/wang8118/article/details/7571744

You may also like...