Ian Usher7109531
Posts: 0
Joined: Tue Sep 16, 2014 6:57 pm

Nearest Locations From a Database.

Hello, i have managed to do this one, but as i havent looked at this for a while im starting to struggle again. I have built an app (getting there) that takes your current lat/long and finds the nearest items to a position.

So far i have used the geolocation service for getting the lat/long of my current location and have used the database query service to work out the nearest two items. (Image DatabaseQueryService)

Image

I have then captured my lat/long as local storage, which im assuming will then need to be fed into the database service before send? But i cant get it to work as it should.

Ive added this javascript:

var whereObject = { "location" : { "$nearSphere" : [parseFloat(currentlat), parseFloat(currentlong)] } };
return JSON.stringify(whereObject);

on the where and allocated the lat/long as can be seen in this image: Image

But when i do this its not returning anything at all, so the code is incorrect somewhere or there is a incorrect mapping.

If i remove the javascript and the mapping of the currentlat currentlong storage's the application just pulls back the information that was fed into the query as shown in the first image which is a set of coorindates i have put in manually.

I have done this before, but cannot see where i am now going wrong, so any help or advice would be greatly appreciated. Ive also made the application public for you to see: https://appery.io/app/mobile-frame?sr...

Many thanks

Ian

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Nearest Locations From a Database.

Hello Ian,

It is impossible to use two mappings arrows to the only one request parameter. Please remove them at all and use the JS code below instead of yours:

pre
var currentlat= Apperyio.storage.currentlat.get();
var currentlong= Apperyio.storage.currentlong.get();
return { "location" : { "$nearSphere" : [currentlat, currentlong] } }/pre

here Storage variables "currentlat" and "currentlong" have to have a data type "number"

Ian Usher8761015
Posts: 0
Joined: Mon Nov 07, 2016 9:11 am

Nearest Locations From a Database.

Excellent, that worked a treat. Thank you.

Return to “Issues”