利用OpenLayer繪制扇形的示例代碼
我在網(wǎng)上看了很多說是繪制扇形的方法,但是我用的時候都不是很好玩,所以說呢,我自己整理了一下,符合了我想要的效果,盡管我能力有限,還是決定分享一下,因為找資料太難了!
我比較懶,就不廢話了,直接上代碼!
創(chuàng)建openlayers地圖
包我就不復制了,根據(jù)官網(wǎng)提供的API自己引用吧!
創(chuàng)建地圖
// 實例化Map map = new Map({ // 創(chuàng)建一個地圖 layers: [new TileLayer({ source: new OSM(), }),], target: 'map', view: new View({ center: fromLonLat([116.400819, 39.916263]), zoom: 15, constrainResolution: true, // 設(shè)置縮放級別為整數(shù) smoothResolutionConstraint: false, // 關(guān)閉無級縮放地圖 }), });
上面代碼大體就這個樣子。
到這兒應該沒什么問題。我用的就是默認的 3857 坐標系,不是4326的。
繪制扇形方法
繪制扇形方法就很簡單了,比如繪制兩個。
// 繪制扇形 addCurve() { let origi_point = fromLonLat([116.410819, 39.916263]); // 繪制扇形的頂點 let circle = this.createRegularPolygonCurve(origi_point, 500, 100, 30, 90) // 調(diào)用繪制扇形的方法得到扇形 let feature = new Feature(circle); // 把扇形加入 feature feature.setStyle( // 設(shè)置一下這個扇形的樣式 new Style({ fill: new Fill({ color: 'rgba(32, 157, 230, 0.3)' }), stroke: new Stroke({ color: 'rgba(255, 205, 67, 0.3)', width: 2 }), }) ) feature.set('type', 'Curve') // 這是給這個扇形添加額外的參數(shù) , 如果是設(shè)置id 用 setId方法 feature.set('curve', { // 這是給這個扇形添加額外的參數(shù),這里的id和 setId的id沒關(guān)系 id: 1, title: '測試001', msg: '測試001-1', msg2: '測試001-2', }) // 創(chuàng)建第二個扇形,和第一個一樣 let circle1 = this.createRegularPolygonCurve(origi_point, 500, 100, 30, 45) let feature1 = new Feature(circle1); feature1.setStyle( new Style({ fill: new Fill({ color: 'rgba(32, 157, 230, 0.3)' }), stroke: new Stroke({ color: 'rgba(255, 205, 67, 0.3)', width: 2 }), }) ) feature1.set('type', 'Curve') feature1.set('curve', { id: 2, title: '超級無敵炫酷爆龍戰(zhàn)神', msg: '超級無敵炫酷爆龍戰(zhàn)神 描述001', msg2: '超級無敵炫酷爆龍戰(zhàn)神 描述002', }) let vectorSource = new VectorSource(); // 創(chuàng)建一個數(shù)據(jù)源 vectorSource.addFeatures([feature, feature1]); // 把兩個扇形加進數(shù)據(jù)源 let vectorLayer = new VectorLayer({ // 創(chuàng)建一個圖層,把數(shù)據(jù)源加進圖層 source: vectorSource }); map.addLayer(vectorLayer); // 把圖層加進地圖 },
接下來就是最重要的, 怎么繪制的扇形,也就是上邊代碼調(diào)用的方法。
/** * APIMethod:OpenLayers繪制扇形的接口擴展 * @param origin 圓心 * @param radius 半徑 * @param sides 邊數(shù) * @param r 弧度 * @param angel 旋轉(zhuǎn)角度(扇形右邊半徑與x正向軸的角度) * @returns {OpenLayers.Geometry.Polygon} */ createRegularPolygonCurve(origin, radius, sides, r, angel) { let rotation = 360 - r; let angle = Math.PI * ((1 / sides) - (1 / 2)); if (rotation) { angle += (rotation / 180) * Math.PI; } let rotatedAngle, x, y; let points = []; for (let i = 0; i < sides; ++i) { let an = i * ((360 - rotation) / 360); rotatedAngle = angle + (an * 2 * Math.PI / sides); x = origin[0] + (radius * Math.cos(rotatedAngle)); y = origin[1] + (radius * Math.sin(rotatedAngle)); points.push([x, y]); } if (rotation != 0) { points.push(origin); } var ring = new LinearRing(points); ring.rotate(angel / 57.3, origin); let list = ring.getCoordinates() return new Polygon([list]); },
好了,就這樣,我是可以了,看你們了!
到此這篇關(guān)于利用OpenLayer繪制扇形的示例代碼 的文章就介紹到這了,更多相關(guān)OpenLayer繪制扇形內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PhantomJS快速入門教程(服務(wù)器端的 JavaScript API 的 WebKit)
Phantom JS是一個服務(wù)器端的 JavaScript API 的 WebKit。其支持各種Web標準: DOM 處理, CSS 選擇器, JSON, Canvas, 和 SVG2015-08-08JS根據(jù)key值獲取URL中的參數(shù)值及把URL的參數(shù)轉(zhuǎn)換成json對象
本篇文章主要圍繞js url 參數(shù)值展開話題,js根據(jù)key值獲取url中的參數(shù)值,接著把url的參數(shù)轉(zhuǎn)換成json,感興趣的朋友一起來學習吧,本文寫的不好地方還望多多指出批評建議2015-08-08