Wikitude Передача значения из Java в JavaScript для использования в AR.RelativeLocation

1

Я передал целое число от java к javascript, и теперь я хочу использовать значение, чтобы изменить высоту объекта.

Я добавил a в RelativeLoction var location = new AR.RelativeLocation(null, 8, 0, a);

Проблема в том, что он игнорирует значение, которое я передал, которое равно 10, и принимает значение 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();

Редактировать после ответа Алекса:

Теперь объект и высота не показывают, не уверены, что я что-то пропустил

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();
Теги:
wikitude

2 ответа

0
Лучший ответ

Мне удалось решить проблему, вот ответ, если кто-то заинтересован

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

    loadValuesFromJava: function loadValuesFromJavaFn(Altitude){
        a = Altitude;
        this.createModelAtLocation();
    },

    /*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;
    /*var a = 0;
    var altitude = 0;

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

//World.init();
//World.loadValuesFromJava();
0

Имейте в виду, что 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;
}
  • 0
    Я попробовал, но сейчас модель и высота не показывают. Для этой строки World.location.altitudeDelta = altitude; Я пробовал также World.location.a = altitude; и получил тот же результат.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню