Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cesium polyline is not rendering, when state variable changes #465

Closed
ats1999 opened this issue Jan 9, 2021 · 5 comments
Closed

Cesium polyline is not rendering, when state variable changes #465

ats1999 opened this issue Jan 9, 2021 · 5 comments
Labels

Comments

@ats1999
Copy link

ats1999 commented Jan 9, 2021

Problem description https://stackoverflow.com/questions/65632650/cesium-polyline-is-not-rendering-when-state-variable-changes

@rot1024
Copy link
Member

rot1024 commented Jan 12, 2021

That question is already answered.

@rot1024 rot1024 closed this as completed Jan 12, 2021
@chriswalz-bg
Copy link

@rot1024 can you link to where it was answered

@rot1024
Copy link
Member

rot1024 commented Jun 18, 2021

Sorry. I didn't read that page in detail, so I misunderstood.

I've read the code, but so far I can't figure out why.

I tried the following code:

import React, { useState } from "react";
import { Cartesian3, Color } from "cesium";
import { Viewer, Entity, PolylineGraphics } from "resium";

const positions = Cartesian3.fromDegreesArrayHeights([0, 0, 1000, 100, 100, 1000]);

const App = () => {
  const [s, ss] = useState(0);
  useEffect(() => {
    const i = setInterval(() => ss(v => v + 1), 1000);
    return () => clearInterval(i);
  }, []);

  return (
    <Viewer full>
      <Entity>
        <PolylineGraphics
          show
          width={3}
          material={Color.RED}
          positions={positions}
        />
      </Entity>
      <div style={{ position: "absolute", left: "0", top: "0", color: "#fff" }}>{s}</div>
    </Viewer>
  );
};

export default App;

Result (Cesium 1.82.1, Resium v1.13.1):

Kapture 2021-06-18 at 11 34 10

As you can see, the polylines are displayed successfully even though there was a state update, so there seems to be no problem with Cesium or Resium itself.

Tip: Cartesian3.fromDegreesArrayHeights(positions) should be placed outside the component. This will reduce the flickering of the screen.

Unfortunately, the cause is not clear. Perhaps the component itself is not displaying, or perhaps there is a mistake in the polyline data. Or maybe your Cesium or Resium is old and you have stepped on some bug (I recommend upgrading).

If you can provide us with the smallest amount of self-contained code that causes the problem, we may be able to clarify the issue further. And in the process, you'll often discover the cause.

@chriswalz-bg
Copy link

@rot1024 The problem for me was using Cartesian3.fromDegreesArrayHeights(positions) inside my functional component. The line wouldn't render at all. Neither would other PolylineGraphics even if their positions were outside of the component.

How do you recommend updating state if you can't use it within the component? Ideally, I'd like to use the useState() hook

@chriswalz-bg
Copy link

Here's a minimal non-working example

import React, { useRef } from 'react'
import { Cartesian3, Color, Viewer as CesiumViewer } from 'cesium'
import { CesiumComponentRef, Entity, PolylineGraphics, Viewer } from 'resium'

const App = () => {
    const viewerRef = useRef<CesiumComponentRef<CesiumViewer>>(null)

    const positionsA = Cartesian3.fromDegreesArrayHeights([-30, 0, 1000, 100, 100, 1000])

    return (
        <>
            <Viewer ref={viewerRef}>
                <Entity>
                    <PolylineGraphics show width={3} material={Color.RED} positions={positionsA} />
                </Entity>
            </Viewer>
        </>
    )
}

export default App

image

And here it is when positionsA is outside the functional component

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants