Start a new topic

calculating the distance to user

calculating the distance to user

hi, I recently started testing wikitude and I think it's the best location based AR sdk in the market right now cause of it's ease of use and it's light . I'm trying to calculate the distance between POIs and the user . I found two functions distanceTo and distanceToUser . if I define a static location and test the distanceTo function it works without any problems . but if I try to use distanceToUser (which I didn't find out how to use it ) like distanceToUser(myGeoLocatin) or myGeoLocation.distanceToUser() none of them work and using it prevents my Json Objects to be loaded . 
I'll be appreciated if you can help me with this . 
thanks
Hi,

following code should work to get the distance to the user:
myGeoLocation = new AR.GeoLocation(objLat,objLon);
var distance = myGeoLocation.distanceToUser();
If this is not working for you I would suggest using the ADE () to verify that your Javascript is executing without an error. If the error persists please post the relevant source parts and/or a simple example that reproduces the problem.
Thanks Wolfgang , I used ADE() but since the  newData() function is being called from native Android code my GeoLocation Objects wasn't created , but it loaded the page without problems . here's the part of newData() function I'm trying to get right :

function newData(jsonData){
                jsonObject = jsonData;
                document.getElementById("statusElement").innerHTML='Loading JSON objects';                         var myLoc = new AR.GeoLocation(47.77317, 13.069929, 320.0);               
for(var i = 0; i                 {
                    geoLoc = new AR.GeoLocation(jsonObject.Point.latitude,jsonObject.Point.longitud e,jsonObject.Point.altitude);
                var distance = myLoc.distanceTo(geoLoc);
                    var poidrawables = new Array();
.
.
.
and if I add that line : var distance = myLoc.distanceTo(geoLoc);
the JsonObjects won't load. is it possible that user location is not defined and the problem cause is that?
thanks
Hi,

I don't see where myLoc variable is initialized. Are you doing this before newData is called? Otherwise it will be undefined and therefore this line throws an exception.

To test the newData function in ADE consider using the Javascript console (e.g. in Chrome -> right click on page -> inspect element ->  console). You should be able to call the method and pass any parameters to it (simply create a default test JSON that you could paste in here).

Alternatively you could add a test button on the html page that calls the newData method with a test JSON.
thanks Wolfgang , myLoc variable is at right side if you scroll to right :) sorry it looks indented in the preview of the post . 
I tested newData in ADE . if I comment out   the code won't have errors . and if I do newData function with sample JSON variable the message "JSON Objects loaded" will be shown at top without problems .
but if I try to run it on my device and include  and comment out 
nothing will be shown and it seems the connection between android native code and ARchitect is lost :(
hehe, that was hidding pretty good ;).

Commenting out the architect.js is not necessary as the error just indicates that the resource is not loadable. This is perfectly okay in the desktop browser. Similarly if you include ade.js after architect.js you don't need to uncomment it for the device (however for releases we suggest removing the ade.js).

Is it possible that you post (attach) or send (info AT wikitude DOT com) the whole architect world and the JSON used for testing?
sure . I emailed it  . thanks so much for your help and time Wolfgang
hello again :) . Wolfgang can you give a little bit more information about distanceToUser() function ? where does it get the user location ? from native android code or somewhere else . I'll be appreciated if you can give me some more information about this function . I looked at docs but there wasn't so much information about this function . 
thanks

I coudl free up some time today and had a look at your problem. The example you sent worked fine. I called newData as follows:

var __data = ;


newData(__data);

distanceToUser is calculated using the current location used by ARchitect. This on Android corresponds to the location you set with ARchitectView.setLocation(...).

Dear Wolfgang.

On basis of the standard WTSimpleARBrowser Example, I used the code mentioned above, but i'm getting quite high numbers back like 10016,  3981.83.
As what should I interprete these, in metres, foot?

Thijs

The distance returned is in meters as stated in the reference documentation. See: http://www.wikitude.com/external/doc/alr/Location.html#method_distanceToUser for more details.
Login or Signup to post a comment