4.透过现象看本质-webgl加载切片

通过canvas加载geojson深入解析添加切片本质

let canvas
let ctx
let height
let width
let lastZoom = 0

const TILE_SIZE = 256
let mapCenter = [108.148732, 36.231006]
let zoom = 7
let minZoom =6
let maxZoom = 17
var resolutions = [1.40782601571, 0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125, 0.02197265625, 0.010986328125, 0.005493164063, 0.002746582031, 0.001373291016, 0.000686645508, 0.000343322754, 0.000171661377, 0.000085830688, 0.000042915344, 0.000021457672, 0.000010728836, 0.000005364418, 0.000002682209, 0.000001341105]
        

function createCanvas() {

    canvas = document.getElementById('canvas');

    canvas.height = window.innerHeight
    canvas.width = window.innerWidth

    height = canvas.height
    width = canvas.width

    ctx = canvas.getContext('2d')
    ctx.translate(width / 2, height / 2)
    initEvent()
}

function initMap() {
    ctx.clearRect(-width / 2, -height / 2, width, height)
//				ctx.save(); //保存初始状态
    
    const centerTile = getTileRowAndCol(...mapCenter, zoom)

    //中心瓦片左上角对应的像素坐标位置
    let centerTilePos = [centerTile[0] * TILE_SIZE, centerTile[1] * TILE_SIZE]

    //中心点对应的像素坐标位置
    let centerPos = getPxFromLngLat(...mapCenter, zoom)

    //差值
    let offset = [centerPos[0] - centerTilePos[0], centerPos[1] - centerTilePos[1]]

    let halfCanvasWidth = canvas.width / 2
    let halfCanvasHeight = canvas.height / 2

    let rowMinNum = Math.ceil((halfCanvasWidth - offset[0]) / TILE_SIZE)
    let colMinNum = Math.ceil((halfCanvasHeight - offset[1]) / TILE_SIZE)
    let rowMaxNum = Math.ceil((halfCanvasWidth - (TILE_SIZE - offset[0])) / TILE_SIZE)
    let colMaxNum = Math.ceil((halfCanvasHeight - (TILE_SIZE - offset[1])) / TILE_SIZE)

    for(let i = -rowMinNum; i <= rowMaxNum; i++) {
        for(let j = -colMinNum; j <= colMaxNum; j++) {
            let row = centerTile[0] + i
            let col = centerTile[1] + j

            let x = i * TILE_SIZE - offset[0]
            let y = j * TILE_SIZE - offset[1]

            gettile(row, col, zoom, y, x)

        }
    }

}

function initEvent() {

    let isMouseDown = false
    canvas.onmousedown = e => {
        if(e.button === 0) {
            isMouseDown = true
        }
    }
    canvas.onmousemove = e => {
        if(!isMouseDown) return

        console.log(e.movementX, 'e.movementX')
        console.log(e.movementY, 'e.movementY')

        let mx = e.movementX * resolutions[zoom]
        let my = e.movementY * resolutions[zoom]

        mapCenter = [mapCenter[0] - mx, mapCenter[1] + my];

        initMap()
    }

    canvas.onmouseup = e => {
        isMouseDown = false
    }

    canvas.onmousewheel = e => {
        if(e.deltaY > 0) {
            if(zoom > minZoom) {
                zoom--
            }
        } else {
            if(zoom < maxZoom) {
                zoom++
            }
        }
        if(lastZoom === zoom) return

        initMap()
    }
}

function getPxFromLngLat(lng, lat, z) {
    const resolution = resolutions[z]
    const x = Math.floor((lng + 180) / resolution)
    const y = Math.floor((90 - lat) / resolution)
    return [y, x]
}

function gettile(row, col, zoom, x, y) {
    let url = "http://10.61.5.99:6009/tdt/SxBaseMap_XX/tile/" + zoom + "/" + row + "/" + col + ".png";
    let img = new Image()
    img.src = url
    img.onload = () => {
        ctx.drawImage(img, x, y)
    }
}

function getTileRowAndCol(lng, lat, z) {

    let resolution = resolutions[z]

    let col = Math.floor((lng + 180) / resolution / 256);
    let row = Math.floor((90 - lat) / resolution * 2 / 256);
    return [row, col]
}
createCanvas()
 window.requestAnimationFrame(initMap)

加载完成如图所示:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫雪giser

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值