Hi all, I would like to include a graticule in the map. I'm using the OL3

Hi all,

I would like to include a graticule in the map. I’m using the OL3 2.2 (which uses openlayers 4.6.5 natively).I’ve added this code in the OLMapConnector:

public native final void initGraticule()/*-{
        
        var graticule = new $wnd.ol.Graticule();
        graticule.setMap(this);
    }-*/;

Calling it after the map is initialized (initMap function in this same class). However, it’s throwing this error in JS:

29VM9047:21 Uncaught TypeError: Cannot read property '0' of null
    at jb (eval at injectScript (localhost/:93625), <anonymous>:21:24)
    at bc (eval at injectScript (localhost/:93625), <anonymous>:37:37)
    at Xk.k.di (eval at injectScript (localhost/:93625), <anonymous>:319:220)
    at K.b (eval at injectScript (localhost/:93625), <anonymous>:46:350)
    at K.Sc.b (eval at injectScript (localhost/:93625), <anonymous>:49:211)
    at lj (eval at injectScript (localhost/:93625), <anonymous>:204:223)
    at kj.bh (eval at injectScript (localhost/:93625), <anonymous>:205:476)
    at K.k.pq (eval at injectScript (localhost/:93625), <anonymous>:135:287)
    at K.eval (eval at injectScript (localhost/:93625), <anonymous>:120:990)

Maybe the map is not yet ready to be included, is it?
Thanks for the help!
Regards,
Ruben

Hi Ruben,

My first hunch would be that “this” refers to the wrong context here. Quick and dirty fix could be to declare your native method in the org.vaadin.gwtol3.client.Map.java and call it from the connector with getWidget().getMap().initGraticule();

Don’t forget to compile both modules when trying out. Please let me know if it helps.

Thanks Matti for answering. I made a mistake when writing as I meant that this code

public native final void initGraticule()/*-{
        var graticule = new $wnd.ol.Graticule();
        graticule.setMap(this);
    }-*/;

was added actually to the Map.java class (gwt-ol3) and it is called from initMap in OLMapConnector.java (v-ol3), as you state. What might it be then? Thanks a lot

Hi Matti,

I could solve it. The thing is that it works for the EPSG:4326 projection (by default) but it doesn’t for customized ones (I am using the 949900 (Mars projection)). Extracted from https://stackoverflow.com/questions/42119977/unable-to-display-the-graticule-with-openlayers-3

Many thanks for your support
Regards,
Ruben

Hi Ruben,

Glad to hear you got it solved!

-Matti