threejs 坐标问题

564 阅读1分钟

确定自己的需求,将经纬度转换为二维平面坐标,墨卡托投影的基础知识要了解,此方法为经纬度转换为墨卡托地图的 x坐标,y坐标的百分比。x,y是0-1小数,用定位去对应地图上的位置。自己想出来的方法,有更好的希望大家可以推荐。

```//经纬度转墨卡托百分比  
function lonlatToMercator(lonLat) {
  const xy = {}
  if (lonLat.x > 180 || lonLat.x == 180) {
    xy.x = ((lonLat.x + 180) % 360) / 360
  } else if (lonLat.x > 0 && lonLat.x < 180) {
    xy.x = (lonLat.x + 180) / 360
  } else if (lonLat.x == 0) {
    xy.x = (lonLat.x + 180) / 360
  } else if (lonLat.x < 0 && lonLat.x > -180) {
    xy.x = (180 + lonLat.x) / 360
  } else {
    xy.x = 1 - ((lonLat.x - 180) % 360) / 360
  }
  if (lonLat.y > 0 || lonLat.y == 0) {
    xy.y = (90 - lonLat.y) / 180
  } else {
    xy.y = (90 - lonLat.y) / 180
  }
  return xy
}
//通过上一步我们已经得出来,经纬度对应的地图上的点,如果我们还要在地图上添加一层三维的东西,还要跟地图对应上,我们可以对应上边的百分比,来计算three中空间坐标的位置。
function getActual(obj) {
  const a = 230.8
  const b = 115.2
  const arr = []
  let c, d
  if (obj.x > 0.5 || obj.x == 0) {
    c = 230.8 * obj.x  - 115.4
  } else {
    c = -(115.4 - 230.8 * obj.x)
  }
  if (obj.y > 0.5 || obj.y == 0.5) {
    d = 115.2 *  obj.y - 57.6
  } else {
    d = -(57.6 - 115.2 * obj.y)
  }
  return [c, d]
}