矢量瓦片相关计算函数

import turf from 'turf'
export default {
  TILE_SIZE: 256,
  /*
  * 获取指定级别的瓦片数目
  */
  _getMapSize(level) {
    return Math.pow(2, level);
  },
  /**
     * Convert a longitude coordinate (in degrees) to the tile X number at a
     * certain zoom level.经度转瓦片列号
     * @param longitude
     *            the longitude coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the tile X number of the longitude value.
  */
   longitudeToTileX(longitude, zoom) {
       let px = this.longitudeToPixelX(longitude, zoom);
       return this.pixelXToTileX(px, zoom);
   },
   /**
      * Convert a latitude coordinate (in degrees) to a tile Y number at a
      * certain zoom level.纬度转瓦片行号
      * @param latitude
      *            the latitude coordinate that should be converted.
      * @param zoom
      *            the zoom level at which the coordinate should be converted.
      * @return the tile Y number of the latitude value.
   */
   latitudeToTileY(latitude, zoom) {
       let py = this.latitudeToPixelY(latitude, zoom);
       return this.pixelYToTileY(py, zoom);
   },
/**
     * Convert a latitude coordinate (in degrees) to a pixel Y coordinate at a
     * certain zoom level.经纬度坐标(纬度)转屏幕像素坐标(Y)
     *
     * @param latitude
     *            the latitude coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the pixel Y coordinate of the latitude value.
     */
    latitudeToPixelY(latitude, zoom) {
        let sinLatitude = Math.sin(latitude * Math.PI / 180);
        return (0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI)) * (this.TILE_SIZE << zoom);
    },
    /**
     * Convert a longitude coordinate (in degrees) to a pixel X coordinate at a
     * certain zoom level.经纬度坐标(经度)转屏幕像素坐标(X)
     *
     * @param longitude
     *            the longitude coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the pixel X coordinate of the longitude value.
     */
    longitudeToPixelX(longitude, zoom) {
        return (longitude + 180) / 360 * (this.TILE_SIZE << zoom);
    },
   /*
   * 指定级别下,将宏观上的经度转换为对应列上的瓦片的像素矩阵列号(微观)
     例如:鼠标点击地图,鼠标位置在点击的瓦片内的行列号(即使像素行列号)。根据该瓦片(png)和计算出的行列号,即可取得鼠标点击位置的像素值。
  */
  _lngToPixelX(longitude, level) {
    let x = (longitude + 180) / 360;
    let pixelX = Math.floor(x * this._getMapSize(level) * 256 % 256);
    return pixelX;
  },
  /*
  * 指定级别纬度对应的像素行号
  */
  _latToPixelY(latitude, level) {
    let sinLatitude = Math.sin(latitude * Math.PI / 180);
    let y = 0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);
    let pixelY = Math.floor(y * this._getMapSize(level) * 256 % 256);
    return pixelY;
  },
  /**
     * Convert a pixel X coordinate to the tile X number.
     * 像素坐标X转瓦片行列号X
     * @param pixelX
     *            the pixel X coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the tile X number.
   */
    pixelXToTileX(pixelX, zoom) {
        return Math.floor(Math.min(Math.max(pixelX / this.TILE_SIZE, 0), Math.pow(2, zoom) - 1));
    },
   /**
     * Converts a pixel Y coordinate to the tile Y number.
     * 像素坐标Y转瓦片行列号Y
     * @param pixelY
     *            the pixel Y coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the tile Y number.
    */
    pixelYToTileY(pixelY, zoom) {
        return Math.floor(Math.min(Math.max(pixelY / this.TILE_SIZE, 0), Math.pow(2, zoom) - 1));
    },
}

你可能感兴趣的:(map,vector,javascript,mapbox)