javascript - Wikitude 将值从 java 传递到 JavaScript 以在 AR.RelativeLocation 中使用它

标签 javascript android wikitude

我将一个整数从 java 传递到 javascript,现在我想使用该值来更改对象的高度。

我向RelativeLoction添加了一个var location = new AR.RelativeLocation(null, 8, 0, a);

问题是它忽略了我传递的值,即 10 并将 a 视为 0。我知道从此值正确传递 var e = document.getElementById('loadingMessage').innerHTML = "Altitude : "+ a; 显示海拔高度:10。

Javascript代码:

var World = {
    loaded: false,
    rotating: false,

    init: function initFn() {
        this.createModelAtLocation();
    },



    createModelAtLocation: function createModelAtLocationFn() {

        /*
            First a location where the model should be displayed will be defined. This location will be relativ to the user.
        */
        var location = new AR.RelativeLocation(null, 8, 0, a);

        /*
            Next the model object is loaded.
        */
        var modelArrow = new AR.Model("assets/arrow.wt3", {
                    onLoaded: this.worldLoaded,
                    scale: {
                        x: 0.005,
                        y: 0.005,
                        z: 0.003
                    },
                    rotate: {
                                    x: 0.0,
                                    y: 90.0,
                                    z: 0.0
                                  },
                                  translate: {
                                      x: 0,
                                      y: 0,
                                      z: 0
                                    }

                });



        var indicatorImage = new AR.ImageResource("assets/indi.png");

        var indicatorDrawable = new AR.ImageDrawable(indicatorImage, 0.1, {
            verticalAnchor: AR.CONST.VERTICAL_ANCHOR.TOP
        });

        /*
            Putting it all together the location and 3D model is added to an AR.GeoObject.
        */
        var obj = new AR.GeoObject(location, {
            drawables: {
               cam: [modelArrow],
               indicator: [indicatorDrawable]
            }
        });
    },

    worldLoaded: function worldLoadedFn() {
        World.loaded = true;
        var e = document.getElementById('loadingMessage').innerHTML = "Altitude: " + a;
        e.parentElement.removeChild(e);
    }
};

    var a = 0;
    var altitude = 0;

       function setAltitude(altitude){
               a = altitude;
           }

World.init();

在 Alex 回答后进行编辑:

现在物体和高度没有显示,不确定我是否遗漏了一些东西

var World = {
    location: null,
    loaded: false,
    rotating: false,

    init: function initFn() {
        this.createModelAtLocation();
    },

    createModelAtLocation: function createModelAtLocationFn() {

        /*

            First a location where the model should be displayed will be defined. This location will be relativ to the user.
        */
        //var location = new AR.RelativeLocation(null, 8, 0, a);
        World.location = new AR.RelativeLocation(null, 8, 0, a);

        /*
            Next the model object is loaded.
        */
        var modelArrow = new AR.Model("assets/arrow.wt3", {
                    onLoaded: this.worldLoaded,
                    scale: {
                        x: 0.005,
                        y: 0.005,
                        z: 0.003
                    },
                    rotate: {
                                    x: 0.0,
                                    y: 90.0,
                                    z: 0.0
                                  },
                                  translate: {
                                      x: 0,
                                      y: 0,
                                      z: 0
                                    }

                });



        var indicatorImage = new AR.ImageResource("assets/indi.png");

        var indicatorDrawable = new AR.ImageDrawable(indicatorImage, 0.1, {
            verticalAnchor: AR.CONST.VERTICAL_ANCHOR.TOP
        });

        /*
            Putting it all together the location and 3D model is added to an AR.GeoObject.
        */
        var obj = new AR.GeoObject(location, {
            drawables: {
               cam: [modelArrow],
               indicator: [indicatorDrawable]
            }
        });
    },

    worldLoaded: function worldLoadedFn() {
        World.loaded = true;
        var e = document.getElementById('loadingMessage').innerHTML = "Altitude: " + a;
        e.parentElement.removeChild(e);
    }
};
    var a = 0;
    var altitude = 0;

       function setAltitude(altitude){
               //a = altitude;
               //World.location.altitudeDelta  = altitude;
               World.location.a = altitude;
           }

World.init();

最佳答案

请记住,Model.onLoaded 是异步的。我的假设是事情发生的顺序是:init() -> createModelAtLocation() -> java 设置海拔 -> worldLoaded()。

为了避免这种情况,您可以采取以下措施:

var World = {
    location: null,
    ...
    World.location = new AR.RelativeLocation(null, 8, 0, a);
    ...
};

function setAltitude(altitude){
   World.location.altitudeDelta = altitude;
}

关于javascript - Wikitude 将值从 java 传递到 JavaScript 以在 AR.RelativeLocation 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377151/

相关文章:

cordova - wikitude 增强现实应用程序使用增强现实

javascript - 为什么没有为 Google HTTPS -> HTTP 删除 Referer header ?

javascript - 如何在 ul 中包装 li 标签组

java - 没有 ViewFlipper 的 Android In/Out 动画转换

java - Activity 生命周期中的 onResume() 和 onPostResume() 有什么区别?

android - Wikitude 如何在同样使用 OpenCV 的项目中使用?

android - Wikitude浏览器如何添加图层?

javascript - 如何将随机生成的数字放入表格中?

javascript - 如何找到browserify的 `require`函数的定义?

android - 如何更改 android 选项卡主机中选定选项卡的颜色?