基于Openlayers 6實(shí)現(xiàn)地圖局部(高亮)優(yōu)雅顯示

修改于2021/1/17

公共地圖資源基本都是全國(guó)范圍或者全球范圍數(shù)據(jù),但具體業(yè)務(wù)需求往往只是需要顯示局部范圍或者局部高亮顯示。本案例基于Openlayers實(shí)現(xiàn)世界地圖內(nèi)蒙古區(qū)域的局部顯示以及呼倫貝爾、阿榮旗的高亮顯示。

1、地圖局部顯示

  1. 加載天地圖影像圖層、道路中文注記圖層。
  2. 道路中文注記圖層渲染后根據(jù)內(nèi)蒙古邊界范圍進(jìn)行局部裁剪。此處使用canvas的復(fù)合操作(globalCompositeOperation),在源圖像中顯示目標(biāo)圖像(destination-in)。
  1. 示例代碼
layer.on("postrender", function (e) {
 let vectorContext = getVectorContext(e);
 e.context.globalCompositeOperation = 'destination-in';
 // feature 為內(nèi)蒙古面要素
 vectorContext.drawFeature(feature, new Style({  
   fill: new Fill({
     color: 'black',// 必需設(shè)置顏色
   })
 }));
 e.context.globalCompositeOperation = 'source-over'; 
});

2、地圖局部高亮

  1. 創(chuàng)建新的畫布,大小與當(dāng)前視圖窗口大小一致。
  2. 新的畫布填充陰影圖形。
  3. 根據(jù)呼倫貝爾或阿榮旗邊界坐標(biāo),在新畫布上繪制區(qū)域圖形。
  4. 新畫布中顯示區(qū)域圖形外的陰影圖形,即區(qū)域圖形透明顯示。
  5. 將新畫布圖形填充到地圖圖層畫布上。
  1. 示例代碼
//points: 呼倫貝爾或阿榮旗邊界坐標(biāo)
//ctx:地圖圖層畫布
heightLight(points, ctx) {
let canvas = ctx.canvas;
let onePageCanvas = document.createElement('canvas');
onePageCanvas.setAttribute('width', String(canvas.width));
onePageCanvas.setAttribute('height', String(canvas.height));
let ctx_ = onePageCanvas.getContext('2d');

ctx_.rect(0, 0, canvas.width, canvas.height);
ctx_.fillStyle = "rgba(3,24,51,0.60)";
ctx_.fill();
ctx_.globalCompositeOperation = 'destination-out';
ctx_.beginPath();
for (let i = 0, cout = coords.length; i < cout; i++) {
  // 獲取屏幕坐標(biāo)
  let screenCoord = this.map.getPixelFromCoordinate(coords[i]);
  let x = screenCoord[0];
  let y = screenCoord[1];
  if (i === 0) {
    ctx_.moveTo(x, y);
  } else {
    ctx_.lineTo(x, y);
    ctx_.lineTo(x, y);
  }
}
ctx_.closePath();
ctx_.fill();
let patten = ctx.createPattern(onePageCanvas, "no-repeat");
ctx.fillStyle = patten;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容