Start a new topic

Distance to user update

Distance to user update


Hi , I have put distance to user information in a label to show to the user . it shows correctly when the ARchitect world launches . but the problem is it doens't update as the user moves . here's the multiplepois.js and marker.js files which I have modified , I call marker function in onLocationChanged function so I think it should update correctly. 

multiplepois.js :

var World = {

 

    markerDrawable_idle1: new AR.ImageResource("assets/marker_idle1.png"),


    markerDrawable_idle2: new AR.ImageResource("assets/marker_idle2.png"),

 


    markerDrawable_directionIndicator: new AR.ImageResource("assets/indi.png"),

 

    // New: a array holding a reference to all marker objects

    markerList: ,

    myMarkers: ,

 

 

    init: function initFn() {

 

        World.myMarkers.push(World.markerDrawable_idle1);

        World.myMarkers.push(World.markerDrawable_idle2);

 

        AR.context.onLocationChanged = World.onLocationChanged;

    },

 

    onLocationChanged: function onLocationChangedFn(latitude, longitude, altitude, accuracy) {

        AR.context.onLocationChanged = null;

 

        // New: Markers are now created using the new operator.

        //      The marker definition is in `marker.js`.

        //      Title, description and location are supplied in the json compatible format

 

        var poiData = {

            "latitude": 35.716124,

            "longitude": 51.366867,

            "altitude": altitude,

            "title": "Marker 0",

            "description": "Description 0",

            "myVar": 0

        };

        World.markerList.push(new Marker(poiData));

 

        poiData = {

            "latitude": 35.716042,

            "longitude": 51.367591,

            "altitude": altitude,

            "title": "Marker 1",

            "description": "Description 1",

            "myVar": 1

        };

        World.markerList.push(new Marker(poiData));

 

    },

 

};

 

World.init();

 

 

and here's the part of marker.js marker function which I use to show the distance to user :

 


    var disLocation = markerLocation.distanceToUser();

    var disLocationString = "";

    if(disLocation>1000)

    {

        disLocationString = Math.round(disLocation/10)/100 + "kms";

    }

    else

    {

        disLocationString = Math.round(disLocation) + "ms";

    }

 



    this.distanceLabel = new AR.Label(disLocationString , 0.7, {

        zOrder : 1,

        offsetX: 0.7,

        scale : 0.5,

        style: {

            textColor : '#FFFFFF'

        }

    });

 

    var markerObject = new AR.GeoObject(markerLocation, {

        drawables: {

            cam: ,

            indicator: this.directionIndicatorDrawable,

            radar: this.radardrawables

        }

 

I'll apprecaite if you can help me with this , everything seems right but it doens't update as the user moves . 

thanks 

You are setting the AR.context.onLocationChanged trigger to null when it is called the first time. Therefore it won't be called again and you will have no possibility to update the labels.

Even if this is not done, your code will create new markers each time the location is updated. Thus there need to be a additional logic to update distance labels.

e.g.

define a new function that is set as the onLocationChanged trigger after it was called the first time. Within this function recalculate the distance and update the labels with the new values: distanceLabel.text = newdistance;

Let me know if you have further questions.

Thanks Wolfgang, I did it as you said by commenting out AR.context.onLocationChanged = null; and by defining a boolean variable to check if it's first time and in the first time I created poi objects. after that I did this to update the distance label of pois :

        var newLocation = new AR.GeoLocation(latitude, longitude, altitude);

        var distanceUpdate = newLocation.distanceToUser();

        for(i=0;i<World.markerList.length;i++)

        {

            var newLocation2 = World.markerList.locationHolder;

            var distanceUpdate2 = newLocation2.distanceToUser();

            var distanceUpdate2String = "";

 

            if(distanceUpdate2>1000)

             {

                distanceUpdate2String = Math.round(distanceUpdate2/10)/100 + "kms";

            }

            else

            {

                distanceUpdate2String = Math.round(distanceUpdate2) + "ms";

            }

 

            World.markerList.distanceLabel.text = distanceUpdate2String;

        }

 

 

Thanks again for your support
Login or Signup to post a comment