You are not logged in.

1

Tuesday, September 24th 2019, 6:38pm

how to access a function in js plugin from xml

Hi,
I have a custom map plugin and I would like to access/call a js function, actually a function from Leaflet.js library, in the plugin. I would like to call this function from the xml file, i.e. from a krpano action called by an event, "onstart" a new scene.
Can anyone tell me how to do that?

Many thanks for help,
Giuseppe

2

Thursday, September 26th 2019, 7:51pm

Hi,
I am asking the same question on reverse:
how to call a js function on a js plugin from a Krpano event (e.g. onnewscene)?
I did this on the plugin.js:

krpano.events[onnewscene] = function() {myFunction()};

function myFunction() {
//js code, i.e. a function from Leaflet
krpano.set(//setting an attribute on a krpano layer according to a value from the previous function ...)
}

but it does not work.

Alternatively I would like to do on krpano (xml file):

<scene name=myscene" onstart="my_Function()">

<action name="my_Function">

<!-- a js function: a leaflet function from the plugin.js -->
set('layer[mylayer].attribute, <!-- a value from the previous js function --> )

</action>

but I do not know how to proceed.

Any suggestions?

Thank you very much for help.

Giuseppe

3

Friday, September 27th 2019, 12:02pm

In the action of type="Javascript" you can try like this

Source code

1
krpano.events.getItem('testevent').onnewscene = "jsget(returnedValue, 'someFunction()');trace('value: ', get(returnedValue));";


This assumes that you have event 'testevent' in the xml and someFunction returns a value

4

Friday, September 27th 2019, 5:06pm

Thank you very much.
I wil give it a try after some time to figure it out what your code means: I am a complete beginner in any coding.
You say to use your code in a action of tipe="javascript": as far as I know jsget, jscall and js can be used in "normal" krpano actions.....

5

Friday, September 27th 2019, 9:02pm

Yes you can use a basic action. I just saw in your code krpano.events... which is used in Javascript type actions.

Depends on your project,
dynamically set onnewscene action in event

Source code

1
set(events['testevent'].onnewscene, 'newsceneaction');


or set in event directly

Source code

1
<events keep="true" name="testevent" onnewscene="newsceneaction" />


The action, which will be executed on new scene


Source code

1
<action name="newsceneaction">jsget(returnedValue, 'someFunction()');trace('value: ', get(returnedValue));</action>

6

Saturday, September 28th 2019, 1:28pm

Thank you very much for explanation!
Not sure to understand correctly:
<action name="newsceneaction">jsget(returnedValue, 'someFunction()');trace('value: ', get(returnedValue));</action>

In my case someFunction() would be a function from the js plugin, i.e. the function map.latLngToContainerPoint() from leaflet.js: it converts latlng geographical coords to x,y coords in the map container.
Just to explain, I have a testing tour with 3 images .

In the xml file I have a plugin/layer linked with a file.js where I built the osm map; a layer for an active spot with an icon and a layer for the radar plugin.
When I change scene (onnewscene) I would like to place the active spot and radar layers on the map layer according to the x,y coords given by the map.latLngToContainerPoint().
I would like to do that with an action on the xml file.
So I need "to pass" the returned value of map.latLngToContainerPoint() (which is a function on the js plugin) to an action on the xml file which is called 'onnewscene'.

7

Saturday, September 28th 2019, 2:38pm

Hi,

I have a custom map plugin and I would like to access/call a js function, actually a function from Leaflet.js library, in the plugin. I would like to call this function from the xml file, i.e. from a krpano action called by an event, "onstart" a new scene.
Can anyone tell me how to do that?


There are several ways - e.g. expose your Javascript function as function on the plugin object:

Source code

1
2
3
4
plugin.my_function = function()
{
  ...
}


When that plugins is included in krpano e.g. this way:

Source code

1
<plugin name="my_plugin" url="my_plugin.js" ... preload="true" />
(preload=true to have it loaded before the newscene event)

Then you can call it from xml this way:

Source code

1
2
3
<events name="my_events" keep="true"
        onnewscene="plugin[my_plugin].my_function();"/
        />
(Important: use an 'own' named <events> element to avoid conflicts with other code when using the global ones)

Or if you want, you can create that event also directly in your plugin:

Source code

1
2
3
4
5
6
7
krpano.set("events[my_events].keep",      true);
krpano.set("events[my_events].onnewscene", my_onnewscene_function);
...
function my_onnewscene_function()
{
  ...
}


For more information please see the documentation about the plugin API:
https://krpano.com/docu/plugininterface/#top

and have also a look into the plugin examples sources provided here:
https://krpano.com/docu/plugininterface/#examples

Best regards,
Klaus

8

Saturday, September 28th 2019, 9:25pm

Klaus,
thank you very much: I will try to implement.
Previously I tried:

krpano.events.onnewscene = function(){ ..

here map.latLngToContainerPoint() a function from Leaflet which converts geographical coods to x.y coords
and code to set x, y coords for an activespot/layer in in the xml
.... }

The above is in the plugin.js file and It works, but perhaps it is not a clean code, so I will change according to your coding.
I would prefer to do the following in xml code :

<scene name="my_ scene" onstart="update_spot_onthemap();">

<action name="update_spot_onthemap">

.............//here I would put in some way map.latLngToContainerPoint() which gives x,y coords of a point on the map

set(layer[activespot].x, 'coord.x');
set(layer[activespot].y, 'coord.y');

</action>

In general (for other use): after plugin.my_function = function(){ } in the plugin.js I can call/use my_function in the xml. Is that correct?


Thank you and best regards,

Giuseppe

Similar threads