1. 使用vite创建一个react的项目

    2. 安装mars3d npm install mars3d -S

    3. 在 vite.config 文件中添加 vite-mars3d-plugin ```typescript import mars3dCesium from “vite-plugin-mars3d”

    plugins: [ mars3dCesium(), ]

    1. 4. 编写组件用于初始化地图
    2. ```tsx
    3. import { useEffect } from "react"
    4. import * as mars3d from "mars3d"
    5. import { getQueryString, isPc } from "@mars/utils/mars-util"
    6. interface MarsMapProps {
    7. url: string
    8. mapKey?: string
    9. options?: any
    10. onLoad?: (map: mars3d.Map) => void
    11. }
    12. function MarsMap(props: MarsMapProps) {
    13. // 使用用户传入的 mapKey 拼接生成 withKeyId 作为当前显示容器的id
    14. const withKeyId = `mars3d-container-${props.mapKey}`
    15. // 用于存放地球组件实例
    16. let map: mars3d.Map // 地图对象
    17. useEffect(() => {
    18. mars3d.Util.fetchJson({ url: props.url }).then((data: any) => {
    19. console.log(data)
    20. initMars3d({
    21. // 合并配置项
    22. ...data.map3d,
    23. ...props.options
    24. })
    25. })
    26. return () => {
    27. map && map.destroy()
    28. }
    29. }, [])
    30. const initMars3d = (option: any) => {
    31. map = new mars3d.Map(withKeyId, option)
    32. // //如果有xyz传参,进行定位
    33. const lat = getQueryString("lat")
    34. const lng = getQueryString("lng")
    35. if (lat && lng) {
    36. map.flyToPoint(new mars3d.LngLatPoint(lng, lat), { duration: 0 })
    37. }
    38. // 开场动画
    39. // map.openFlyAnimation();
    40. // //针对不同终端的优化配置
    41. if (isPc()) {
    42. // Cesium 1.61以后会默认关闭反走样,对于桌面端而言还是开启得好,
    43. map.scene.postProcessStages.fxaa.enabled = true
    44. map.zoomFactor = 2.0 // 鼠标滚轮放大的步长参数
    45. // IE浏览器优化
    46. if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 0) {
    47. map.viewer.targetFrameRate = 20 // 限制帧率
    48. map.scene.requestRenderMode = true // 取消实时渲染
    49. }
    50. } else {
    51. map.zoomFactor = 5.0 // 鼠标滚轮放大的步长参数
    52. map.scene.screenSpaceCameraController.enableTilt = false
    53. // 移动设备上禁掉以下几个选项,可以相对更加流畅
    54. map.scene.requestRenderMode = true // 取消实时渲染
    55. map.scene.fog.enabled = false
    56. map.scene.skyAtmosphere.show = false
    57. map.scene.globe.showGroundAtmosphere = false
    58. }
    59. // //二三维切换不用动画
    60. if (map.viewer.sceneModePicker) {
    61. map.viewer.sceneModePicker.viewModel.duration = 0.0
    62. }
    63. // webgl渲染失败后,刷新页面
    64. map.on(mars3d.EventType.renderError, async () => {
    65. alert("程序内存消耗过大,请重启浏览器")
    66. window.location.reload()
    67. })
    68. // map构造完成后的一些处理
    69. onMapLoad()
    70. props.onLoad(map)
    71. }
    72. // map构造完成后的一些处理
    73. function onMapLoad() {
    74. // Mars3D地图内部使用,如右键菜单弹窗
    75. // @ts-ignore
    76. // window.globalAlert = $alert
    77. // @ts-ignore
    78. // window.globalMsg = $message
    79. // 用于 config.json 中 西藏垭口 图层的详情按钮 演示
    80. // @ts-ignore
    81. window.showPopupDetails = (item: any) => {
    82. alert(item.NAME)
    83. }
    84. // 用于 config.json中配置的图层,绑定额外方法和参数
    85. const tiles3dLayer = map.getLayer(204012, "id") // 上海市区
    86. if (tiles3dLayer) {
    87. tiles3dLayer.options.onSetOpacity = function (opacity: number) {
    88. tiles3dLayer.style = {
    89. color: {
    90. conditions: [
    91. ["${floor} >= 200", "rgba(45, 0, 75," + 0.5 * opacity + ")"],
    92. ["${floor} >= 100", "rgba(170, 162, 204," + opacity + ")"],
    93. ["${floor} >= 50", "rgba(224, 226, 238," + opacity + ")"],
    94. ["${floor} >= 25", "rgba(252, 230, 200," + opacity + ")"],
    95. ["${floor} >= 10", "rgba(248, 176, 87," + opacity + ")"],
    96. ["${floor} >= 5", "rgba(198, 106, 11," + opacity + ")"],
    97. ["true", "rgba(127, 59, 8," + opacity + ")"]
    98. ]
    99. }
    100. }
    101. }
    102. }
    103. }
    104. return <div id={withKeyId} className="mars3d-container"></div>
    105. }
    106. export default MarsMap
    1. 使用组件创建地球场景 ```tsx import React, { useContext } from “react” import ReactDOM from “react-dom”

    import “mars3d/dist/mars3d.css”

    const configUrl = ${process.env.BASE_URL}config/config.json

    const marsOnload = map: Map) => { console.log(“map构造完成”, map) }

    ReactDOM.render( , document.getElementById(“root”) )

    ```