亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Canvas中繪制Geojson數(shù)據(jù)示例詳解

 更新時(shí)間:2022年11月17日 09:54:58   作者:uccs  
這篇文章主要為大家介紹了Canvas中繪制Geojson數(shù)據(jù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

需求分析

在做地圖開發(fā)的時(shí)候遇到一個(gè)需求,是在 canvas 中繪制 Geojson 數(shù)據(jù)

數(shù)據(jù)格式為 EPSG:4326Polygon

  • 三維數(shù)組
  • 每一項(xiàng)都是由經(jīng)緯度組成的
  • 第一個(gè)點(diǎn)和最后一個(gè)點(diǎn)相同,表示 Polygon 是閉合的
[
  [
    [109.54420471485196, 35.76192112844663],
    [109.54423617129702, 35.76132766033574],
    [109.54539219590997, 35.76155739029704],
    [109.54521918540507, 35.76241249100947],
    [109.54420471485196, 35.76192112844663],
  ],
];

需求分析:

  • 顯示在 canvas 的中間
  • 地圖 y 軸和屏幕的 y 軸是相反的,繪制出來的 Polygon 不能和實(shí)際反過來
    • 屏幕的原點(diǎn)在左上角,地圖的原點(diǎn)在左下角

數(shù)據(jù)處理

widthheightcanvas 的寬高

將經(jīng)度和緯度單獨(dú)拆分出來

const getLongitudesAndLatitudes = (coordinates: number[][][]) => {
  return coordinates[0].reduce(
    (pre, cur) => {
      pre.longitudes.push(cur[0]);
      pre.latitudes.push(cur[1]);
      return pre;
    },
    { longitudes: [] as number[], latitudes: [] as number[] }
  );
};

得到的結(jié)果是:

const longitudes = [
  109.54420471485196, 109.54423617129702, 109.54539219590997,
  109.54521918540507, 109.54420471485196,
];
const latitudes = [
  35.76192112844663, 35.76132766033574, 35.76155739029704, 35.76241249100947,
  35.76192112844663,
];

計(jì)算縮放比例

width / (Math.max(longitudes) - Math.min(longitudes)) 的作用計(jì)算 Polygonx 軸縮放比例 xScale = 67369.49567346855

height 同理,計(jì)算出 y 軸的縮放比例 yScale = 12905.23981205731

總的縮放比例 scale 采用 xScale、yScale 誰小用誰,為 scale = 12905.23981205731,因?yàn)橛眯〉目s放比例,才能在有限的空間下顯示全

const calcScale = ({
  longitudes,
  latitudes,
}: {
  longitudes: number[];
  latitudes: number[];
}) => {
  const xScale =
    width / Math.abs(Math.max(...longitudes) - Math.min(...longitudes));
  const yScale =
    height / Math.abs(Math.max(...latitudes) - Math.min(...latitudes));
  return xScale < yScale ? xScale : yScale;
};

計(jì)算偏移度

(Math.max(longitudes) - Math.min(longitudes)) * scale 作用是將經(jīng)度按照 scale 進(jìn)行縮放,緯度也是同理

在用 widthheight 去減,分別得到要 x 軸和 y 軸需要偏移的空間

這些空間要分布在兩邊,也就是說要分布 Polygon的周圍,所以左后需要除 2,最終得到 xOffset = 32.33763608704606,yOffset = -8.881784197001252e-16

const calcOffset = (
  { longitudes, latitudes }: { longitudes: number[]; latitudes: number[] },
  scale: number
) => {
  const xOffset =
    (width -
      Math.abs(Math.max(...longitudes) - Math.min(...longitudes)) * scale) /
    2;
  const yOffset =
    (height -
      Math.abs(Math.max(...latitudes) - Math.min(...latitudes)) * scale) /
    2;
  return { xOffset, yOffset };
};

將 Coordinates 進(jìn)行縮放

對(duì) coordinates 縮放一共分為 3

比較難理解的是第一步,為什么要將每個(gè)點(diǎn)去減它的最小值 item[0] - Math.min(...longitudes)

這樣做的目的是為了那個(gè)點(diǎn)無限接近于原點(diǎn),接近原點(diǎn)后,再通過乘以縮放比例和加上偏移度,得到最終的的縮放值

在需求分析時(shí)說了,屏幕的原點(diǎn)在左上角,地圖的原點(diǎn)在左下角,所以 y 軸是剛好是相反,y 軸就用 Math.max(...latitudes) - item[1]

const scaleCoordinates = (
  coordinates: number[][][],
  scale: number,
  offset: ReturnType<typeof calcOffset>,
  { longitudes, latitudes }: { longitudes: number[]; latitudes: number[] }
) => {
  return coordinates[0]
    .map((item) => {
      item[0] = item[0] - Math.min(...longitudes);
      item[1] = Math.max(...latitudes) - item[1];
      return item;
    })
    .map((item) => {
      item[0] = item[0] * scale;
      item[1] = item[1] * scale;
      return item;
    })
    .map((item) => {
      item[0] = item[0] + offset.xOffset;
      item[1] = item[1] + offset.yOffset;
      return item;
    });
};

使用 Canvas 進(jìn)行繪制

const canvas = document.getElementById("canvas");
if (!canvas.getContext) return;
const ctx = canvas!.getContext("2d")!;
ctx.fillStyle = "rgba(32, 210, 255, 0.3)";
ctx.strokeStyle = "rgba(32, 210, 255, 1)";
ctx.beginPath();
ctx.moveTo(coordinates[0][0], coordinates[0][1]);
for (let i = 1; i < coordinates.length; i++) {
  ctx.lineTo(coordinates[i][0], coordinates[i][1]);
}
ctx.closePath();
ctx.stroke();
ctx.fill();

存在問題

無法區(qū)分 Polygon 大小,所以這個(gè)方案適用于繪制縮略圖

以上就是Canvas中繪制Geojson數(shù)據(jù)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Canvas繪制Geojson數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • JS使用AudioContext實(shí)現(xiàn)音頻流實(shí)時(shí)播放

    JS使用AudioContext實(shí)現(xiàn)音頻流實(shí)時(shí)播放

    這篇文章主要為大家詳細(xì)介紹了JavaScript如何使用AudioContext實(shí)現(xiàn)音頻流實(shí)時(shí)播放功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 輸入密碼時(shí)檢測(cè)大寫是否鎖定的js代碼

    輸入密碼時(shí)檢測(cè)大寫是否鎖定的js代碼

    網(wǎng)站登錄為了更好的用戶體驗(yàn)都會(huì)在輸入密碼的時(shí)候檢測(cè)是否開啟大寫。提醒用戶。
    2011-02-02
  • JavaScript正則表達(dá)式實(shí)例詳解

    JavaScript正則表達(dá)式實(shí)例詳解

    在編寫處理字符串的程序或網(wǎng)頁時(shí),經(jīng)常會(huì)有查找符合某些復(fù)雜規(guī)則的字符串的需要。正則表達(dá)式就是用于描述這些規(guī)則的工具。換句話說,正則表達(dá)式就是記錄文本規(guī)則的代碼。
    2016-10-10
  • webpack4.x CommonJS模塊化淺析

    webpack4.x CommonJS模塊化淺析

    這篇文章主要介紹了webpack4.x CommonJS模塊化淺析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • javaScript矢量圖表庫(kù)-gRaphael幾行代碼實(shí)現(xiàn)精美的條形圖/餅圖/點(diǎn)圖/曲線圖

    javaScript矢量圖表庫(kù)-gRaphael幾行代碼實(shí)現(xiàn)精美的條形圖/餅圖/點(diǎn)圖/曲線圖

    gRaphael是一個(gè)致力于幫助開發(fā)人員在網(wǎng)頁中繪制各種精美圖表的 Javascript庫(kù),你只需要編寫幾行簡(jiǎn)單的代碼就能創(chuàng)建出精美的條形圖、餅圖、點(diǎn)圖和曲線圖,感興趣的朋友可以了解下
    2013-01-01
  • Three.js加載外部模型的教程詳解

    Three.js加載外部模型的教程詳解

    這篇文章主要介紹了Three.js外部模型加載的教程詳解,在文章給大家補(bǔ)充介紹了three.js 外部模型加載json的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11
  • js propertychange和oninput事件

    js propertychange和oninput事件

    項(xiàng)目中常遇到輸入框檢查的需求,比如即時(shí)搜索,用change事件?change事件失去焦點(diǎn)才發(fā)生,無法做到即時(shí)。keypresss事件?能監(jiān)聽到鍵盤,但監(jiān)聽不到鼠標(biāo)復(fù)制粘貼,不完美
    2014-09-09
  • Next.js應(yīng)用轉(zhuǎn)換為TypeScript方法demo

    Next.js應(yīng)用轉(zhuǎn)換為TypeScript方法demo

    這篇文章主要為大家介紹了Next.js應(yīng)用轉(zhuǎn)換為TypeScript方法demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • JS實(shí)現(xiàn)拖動(dòng)模態(tài)框案例

    JS實(shí)現(xiàn)拖動(dòng)模態(tài)框案例

    這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)拖動(dòng)模態(tài)框案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)與數(shù)據(jù)傳遞方案

    小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)與數(shù)據(jù)傳遞方案

    在開發(fā)過程中經(jīng)常會(huì)遇到在微信小程序的頁面跳轉(zhuǎn)以及數(shù)據(jù)傳遞的知識(shí)點(diǎn),所以下面這篇文章主要給大家介紹了關(guān)于小程序?qū)崿F(xiàn)頁面跳轉(zhuǎn)與數(shù)據(jù)傳遞的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09

最新評(píng)論