项目中用到了地图展示,但是有个需求是根据参数画扇形,但是OpenLayers没有画扇形的现成函数,在某个牛人的帮助下,修改了下源代码便可以了
1,首先在源码中加入以下代码,最好是在OpenLayers.Geometry.Polygon.createRegularPolygon这个函数的源代码后面加入:
- 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
- );
-
-
- var polygonFeature = new OpenLayers.Feature.Vector(
- sector,
- {
- 'id':'sector_',
- 'type':'sector',
- 'name':'join'
- });
- vector_site_layer.addFeatures(polygonFeature);
大功告成,当然还可以根据扇形的参数不同显示不同的颜色。