index.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React from 'react'
  2. import { Color } from "csstype"
  3. import * as Cesium from 'cesium'
  4. import createMVTWithStyle from './mvt'
  5. import style from './style.module.css'
  6. declare global {
  7. interface Window { CESIUM_BASE_URL: any; ol: any }
  8. }
  9. window.CESIUM_BASE_URL = './'
  10. const ol = window.ol
  11. export interface LayerStyle {
  12. lineColor: Color,
  13. lineWidth: number,
  14. fillColor: Color
  15. }
  16. interface Props extends LayerStyle {
  17. url: string,
  18. lng: number,
  19. lat: number,
  20. height: number
  21. }
  22. interface State {
  23. viewer: any
  24. }
  25. function createStyle (style: LayerStyle) {
  26. return new ol.style.Style({
  27. fill: new ol.style.Fill({
  28. color: style.fillColor,
  29. // stroke:"green"
  30. }),
  31. stroke: new ol.style.Stroke({
  32. color: style.lineColor,
  33. width: style.lineWidth,
  34. lineCap: "round",
  35. }),
  36. });
  37. }
  38. class VectorView extends React.Component<Props, State> {
  39. clipLayer: any
  40. constructor(props: Props) {
  41. super(props)
  42. this.state = { viewer: null }
  43. }
  44. componentDidUpdate () {
  45. if (!this.state.viewer) return;
  46. if (this.clipLayer) {
  47. this.state.viewer.imageryLayers.remove(this.clipLayer, true)
  48. }
  49. this.clipLayer = createMVTWithStyle(Cesium, ol, {
  50. url: this.props.url,
  51. style: createStyle(this.props)
  52. }) as any;
  53. this.state.viewer.imageryLayers.addImageryProvider(this.clipLayer);
  54. }
  55. componentDidMount () {
  56. let viewer = new Cesium.Viewer('cesiumContainer', {
  57. baseLayerPicker: false,
  58. geocoder: false,
  59. homeButton: false,
  60. sceneModePicker: false,
  61. timeline: false,
  62. animation: false,
  63. navigationHelpButton: false,
  64. });
  65. viewer.camera.flyTo({
  66. duration: 1,
  67. destination: Cesium.Cartesian3.fromDegrees(this.props.lng, this.props.lat, this.props.height)
  68. });
  69. viewer.scene.globe.baseColor = new Cesium.Color(1.0, 1.0, 1.0, 1.0);
  70. this.setState({ viewer })
  71. }
  72. componentWillUnmount() {
  73. if (this.state.viewer) {
  74. this.state.viewer.imageryLayers.remove(this.clipLayer, true)
  75. this.state.viewer.destroy()
  76. }
  77. }
  78. render() {
  79. return <div id="cesiumContainer" className={style.layer}></div>
  80. }
  81. }
  82. export default VectorView