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

Can't addOverlay #1

Open
cyango opened this issue Feb 17, 2021 · 7 comments
Open

Can't addOverlay #1

cyango opened this issue Feb 17, 2021 · 7 comments

Comments

@cyango
Copy link

cyango commented Feb 17, 2021

Hi,

Using create-react-app with hooks. Map loads well, but calling addOverlay doesn't seem to work. The blue-fot.png is on public folder root.

  const aref = useRef();

  const featureCollection = {
    type: 'FeatureCollection',
    features: [
      {
        geometry: {
          type: 'Point',
          coordinates: [13.6, 47.23, 2500],
        },
        type: 'Feature',
        id: 0,
        properties: {
          image: './blue-dot.png',
        },
      },
    ],
  };

  useEffect(() => {
    console.log(aref);
    if (aref.current) {
      // @ts-ignore
      aref.current.addOverlay(featureCollection);
      console.log('addd');
      // @ts-ignore
      // aref.current.onOverlayAdded = function (name) {
      //   console.log('Overlay added:', name);
      // };
    }
  }, [aref]);

...

 <ProceduralMap
        ref={aref}
        datasource={datasource}
        compassVisible={false}
        displayLocation={{
          latitude: 47.23,
          longitude: 13.6,
        }}
      />

@felixpalmer
Copy link
Owner

Could you share a jsfiddle or some other sort of live example?

@cyango
Copy link
Author

cyango commented Feb 28, 2021

This is the current ProceduralMap object I get from react component
image

As you can see there are lots of functions missing like onOverlayAdded...etc.

I can add a Basic Line, but can't add marker and text marker or image markers, following the same logic on the documentation.

@felixpalmer
Copy link
Owner

I've updated the example repo to show adding an overlay and responding to clicks on features. It works fine for me with an image marker.

The relevant part is here:

export default class Example extends React.Component {
  constructor( props ) {
    super( props );
    this.map = React.createRef();
  }

  componentDidMount() {
    // Once map is initialized add an overlay
    const map = this.map.current;
    map.addOverlay( overlay );
  }

  render() {
    return React.createElement( ProceduralMap, {
      ref: this.map,
      datasource,

      // Configure UI elements to show
      compassVisible: true,

      // Configure initial map location
      displayLocation: {
        latitude: 47.25,
        longitude: 13.56
      },

      // Define callbacks
      onFeatureClicked: id => {
        // The id passed here is defined in the overlay
        const map = this.map.current;

        // Simply focus on the feature when clicked
        map.focusOnFeature( id );
      }
    } );
  }
} 

As to your other point, the onOverlayAdded etc functions are missing because you haven't defined them. They are callbacks that you define on the map component. If they are not defined then it is expected that they will not be present and thus they won't get called. Again, see the above example to see how to define these.

@cyango
Copy link
Author

cyango commented Mar 4, 2021

Hum...strange, I'm using React with hooks. I'm unable to get the same results:

import overlay from "overlay.js"
.
.

const component =()=>{
.
.
.
const NASADEM_APIKEY = "xxxxxxxxxxxxxxx";
  const MAPTILER_APIKEY = "xxxxxxxxxxx";

  const datasource = {
    elevation: {
      apiKey: NASADEM_APIKEY,
    },
    imagery: {
      apiKey: MAPTILER_APIKEY,
      urlFormat: "https://api.maptiler.com/tiles/satellite/{z}/{x}/{y}.jpg?key={apiKey}",
      attribution:
        "<a href="https://www.maptiler.com/copyright/">Maptiler</a> <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors",
    },
  };

  const aref = createRef();

  useEffect(() => {
    if (aref.current) {
      aref.current.addOverlay(overlay);

      aref.current.onOverlayAdded = function (name) {
        console.log("Overlay added:", name);
      };
    }
  }, []);

  return (
    <div>
      <ProceduralMap
        ref={aref}
        datasource={datasource}
        compassVisible={false}
        displayLocation={{
          latitude: 47.25,
          longitude: 13.56,
        }}
      />
    </div>
  );
};

I can see the maps, but no added custom overlay. This same approach works with the basic line however.

@felixpalmer
Copy link
Owner

Without seeing what you have in overlay.js I can't help debug this. That why it would be useful to be able to take a look at a live example. My guess is that there is something wrong with the overlay you've defined

@cyango
Copy link
Author

cyango commented Mar 5, 2021

Sorry, I'm using the exact same overlay.js file you have here: https://github.com/felixpalmer/procedural-gl-react-example/blob/main/overlay.js

@cyango
Copy link
Author

cyango commented Mar 6, 2021

Ok, so I found a solution, but I think this should be included on documentation or the component itself.

The font awesome css was missing on my index.html

   <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">

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

No branches or pull requests

2 participants