Skip to content

Instantly share code, notes, and snippets.

@troyz
Created October 19, 2017 03:14
Show Gist options
  • Select an option

  • Save troyz/41d392b49bca04fec2cc333fa6e78cf7 to your computer and use it in GitHub Desktop.

Select an option

Save troyz/41d392b49bca04fec2cc333fa6e78cf7 to your computer and use it in GitHub Desktop.

Revisions

  1. troyz created this gist Oct 19, 2017.
    55 changes: 55 additions & 0 deletions tile_caculator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    /**
    * 由18/19级的左上角/右下角瓦片,推导出20/21级的瓦片编码规则。
    * 依赖于[tile-lnglat-transform](https://github.com/CntChen/tile-lnglat-transform)
    */

    var TileLnglatTransform = require('../builds/index');

    var TileLnglatTransformBaidu = TileLnglatTransform.TileLnglatTransformBaidu;

    // 可以通过百度web地图获取18级的瓦片
    let level = 18;
    let data = {
    [level]: {
    // 左上角瓦片坐标
    leftTopTile: {
    tileX: 48566,
    tileY: 19307,
    },
    // 右下角瓦片坐标
    rightBottomTile: {
    tileX: 48570,
    tileY: 19303,
    },
    }
    };

    // 获取瓦片左上角点的经纬度
    function getLeftTopPixelLatLng(level)
    {
    let leftTopTile = data[level].leftTopTile;
    return TileLnglatTransformBaidu.pixelToLnglat(5, 251, leftTopTile.tileX, leftTopTile.tileY, level);
    }

    // 获取瓦片右下角点的经纬度
    function getRightBottomPixelLatLng(_level)
    {
    let rightBottomTile = data[_level].rightBottomTile;
    return TileLnglatTransformBaidu.pixelToLnglat(251, 5, rightBottomTile.tileX, rightBottomTile.tileY, _level);
    }

    for(let _level = level + 1; _level <= 21; _level++)
    {
    let leftTopLatLng = getLeftTopPixelLatLng(_level - 1);
    let rightBottomLatLng = getRightBottomPixelLatLng(_level - 1);

    let leftTopTile = TileLnglatTransformBaidu.lnglatToTile(leftTopLatLng.lng, leftTopLatLng.lat, _level);
    let rightBottomTile = TileLnglatTransformBaidu.lnglatToTile(rightBottomLatLng.lng, rightBottomLatLng.lat, _level);

    data[_level] = {
    leftTopTile: leftTopTile,
    rightBottomTile: rightBottomTile,
    };
    }

    console.log(JSON.stringify(data, null, 2));