Three Vue3Three Vue3
指南
在线演示
AI 智能体
  • English
  • 简体中文
GitHub
指南
在线演示
AI 智能体
  • English
  • 简体中文
GitHub
    • 快速开始
    • 场景
    • 参数总线
    • 天空盒
    • 模型加载器

      • 组件加载器
      • 函数加载器
    • 弹出窗口
    • 标注
    • 动态元素
    • 动画
    • 模型旋转器
    • 拾取
    • 相机自适应
    • 网格模型

      • 波动圆
      • 流动线网格
    • 效果

      • 自然

        • 雨
        • 雪
        • 海洋水面
        • 互动水池
        • 沙滩漫步
      • 天空与大气

        • 天空穹顶
        • 高度雾
        • 星云星系
      • 光照

        • 泛光
        • 扫光
        • 灯光渐变
        • 光柱
        • 雷达扫描
        • 信号波
        • 力场护盾
      • 粒子与精灵

        • 烟花
        • 文字烟花
        • 光晕精灵
    • 地理

      • 地理参考系
      • GeoObject 地理对象
        • 默认用法
        • 锚定已有对象
        • Props
        • 事件
      • GeoJsonLayer 图层
      • 瓦片图层

类型

组件

默认用法

GeoObject 将一个 Object3D 锚定到地理坐标上。不传 object 时,组件会创建一个空的 THREE.Group 并通过 onReady 交给你,你可以在其中添加标记、标签或已加载的模型。更新 coordinate 或 datum 时会直接移动物体,不会重新构建。

点击我查看代码
<template>
  <tv-scene
    class="scene"
    bg-color="rgb(40, 42, 54)"
    :origin="ORIGIN"
    :grid-helper="GRID"
    @created="created"
  >
    <GeoObject
      v-for="(marker, index) in MARKERS"
      :key="index"
      :coordinate="marker"
      datum="GCJ02"
      :object="anchors[index]"
    />
  </tv-scene>
</template>

<script lang="ts" setup>
import { onBeforeUnmount, shallowRef } from 'vue'
import * as THREE from 'three'
import { GeoObject } from 'three-vue3'
import type { GeoPoint } from 'three-vue3'

// Scene 原点使用 WGS84;标记点使用高德/腾讯等服务返回的 GCJ02 坐标。
const ORIGIN: GeoPoint = { lng: 116.391, lat: 39.907 }
const GRID = { size: 1400, divisions: 28, colorCenterLine: 0x4a6fa5, colorGrid: 0x2f3e5c }
const MARKERS: GeoPoint[] = [
  { lng: 116.397428, lat: 39.90923 },
  { lng: 116.4005, lat: 39.9117 },
  { lng: 116.3945, lat: 39.912 }
]

// 锚点统一在 scene 的 created 回调中创建,绝不在模块作用域创建。
const anchors = shallowRef<THREE.Object3D[]>([])

const created = (scene, { camera }) => {
  // 从南侧抬高的视角向北俯视标记点。
  scene.setPosition(camera, { x: 0, y: 750, z: 1000 })
  camera.lookAt(0, 0, 0)

  anchors.value = MARKERS.map(() => {
    const anchor = new THREE.Group()
    const cone = new THREE.Mesh(
      new THREE.ConeGeometry(40, 120, 24),
      new THREE.MeshBasicMaterial({ color: 0xffd166 })
    )
    cone.position.y = 60
    anchor.add(cone)
    return anchor
  })
}

onBeforeUnmount(() => {
  // 外部传入的对象由示例自行释放。
  anchors.value.forEach((anchor) => {
    anchor.removeFromParent()
    anchor.traverse((child) => {
      const mesh = child as THREE.Mesh
      mesh.geometry?.dispose()
      ;(mesh.material as THREE.Material | undefined)?.dispose()
    })
  })
  anchors.value = []
})
</script>

锚定已有对象

传入自己的 Object3D 时,对象的归属仍属于调用方:组件会把它重新挂到 Scene 下,卸载时再恢复到原来的父节点。

<script lang="ts" setup>
import * as THREE from 'three'
import { GeoObject } from 'three-vue3'
import type { GeoPoint } from 'three-vue3'

const coordinate: GeoPoint = { lng: 116.397, lat: 39.909 }
const model = new THREE.Group()
</script>

<template>
  <GeoObject :coordinate="coordinate" :object="model" />
</template>

Props

名称类型默认值描述
coordinateGeoPoint—地理锚点,采用 datum 声明的基准面
datum'WGS84' | 'GCJ02' | 'BD09'Scene 的 datumcoordinate 所使用的基准面
objectTHREE.Object3D自动创建 Group要锚定的已有对象
onReady(object: THREE.Object3D) => void—对象就绪后回调一次
sceneTHREE.Scene上下文中的 scene非 Scene 用法下显式指定场景

事件

名称参数描述
onReady(object: THREE.Object3D) => void对象被添加到场景并完成定位后触发
在 GitHub 上编辑此页
Last Updated:
Contributors: Shing Rui
Prev
地理参考系
Next
GeoJsonLayer 图层