Sie sind nicht angemeldet.

Lieber Besucher, herzlich willkommen bei: krpano.com Forum. Falls dies Ihr erster Besuch auf dieser Seite ist, lesen Sie sich bitte die Hilfe durch. Dort wird Ihnen die Bedienung dieser Seite näher erläutert. Darüber hinaus sollten Sie sich registrieren, um alle Funktionen dieser Seite nutzen zu können. Benutzen Sie das Registrierungsformular, um sich zu registrieren oder informieren Sie sich ausführlich über den Registrierungsvorgang. Falls Sie sich bereits zu einem früheren Zeitpunkt registriert haben, können Sie sich hier anmelden.

1

Dienstag, 24. September 2019, 18:38

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

Donnerstag, 26. September 2019, 19:51

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

Freitag, 27. September 2019, 12:02

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

Quellcode

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

Freitag, 27. September 2019, 17:06

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

Freitag, 27. September 2019, 21:02

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

Quellcode

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


or set in event directly

Quellcode

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


The action, which will be executed on new scene


Quellcode

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

6

Samstag, 28. September 2019, 13:28

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

Samstag, 28. September 2019, 14:38

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:

Quellcode

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


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

Quellcode

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:

Quellcode

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:

Quellcode

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

Samstag, 28. September 2019, 21:25

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