javascriptkrpano

How to retrieve hotspot from KRPANO using Javascript


I wan't to retrieve a hotspot from KRPANO using a javascript call.

With the below content I retrieve undefined.

KRPANO XML snippet:

<hotspot name="spot0" style="hotspot_ani_white" ath="-25" atv="-10" />   
<hotspot name="spot1" style="hotspot_ani_white" ath="-25" atv="-10" />

External javascript file loaded via index.html:

$(function() {

  var krpano = $('#krpanoSWFObject')[0];
  var spotName = 'spot0';
  curSpot = krpano.get(hotspot[spotName]);
  console.log(curSpot)

});

Any help is welcome. Thanks in advance!


Solution

  • You are close to it. But I'm not sure your can get the element as you wrote it.

    First, you should grab the KRPano plugin as an object and not as HTML DOM element:

        var myKRPano = $('#krpanoSWFObject'); // First way, no need of "[0]"
        var otherKRPano = document.getElementById("krpanoSWFObject"); // Another way
    

    Now you got your JS object, just call "get" method. BUT you should know that "get()" executes internal code: thus you have to place your request as a string! For instance, and with your own code:

        var spotname = "spot0";
        var query = "hotspot['"+spotname+"']"; // i.e. "hotspot['spot0']"
        var myHotspot = myKRPano.get(query); // or myKRPano.get("hotspot['spot0']");
    

    This will provide you with something like:

        Object { _type="hotspot",  DATA={...},  plugin={...},  plus...}
    

    From there, you can ask for

        > myHotspot.atv
          -10.5868612
        > myHotspot.name
          "spot0"
    

    Don't hesitate to use a debug tool for your browser when working with KRPano, as a general advice. Then you can directly try your code and get the issue. If you do so, you would see that "$('#krpanoSWKObject').get()" is set while "$('#krpanoSWKObject')[0].get()" is not. :)

    Regards,