Hey,
I am having a hard time getting $maxDistance to work. I pass the following object to the "where" parameter in the cloud search:
var searchWhere = { lnglat = { "$maxDistance" = 50; "$nearSphere" = [ 40.72760009765625, -74.00137329101562 ]; }; }And the list of results still returns a location which is across the country (US) from where I am.
I read in another question here that $nearSphere needs floating numbers to work and that's what I'm passing -- my proof is that Ti.API.info(typeof searchWhere.lnglat['$nearSphere'][0]); returns "number" -- but Ti.API.info(searchWhere) prints this object:
{ lnglat = { "$maxDistance" = 50; "$nearSphere" = ( "40.72760009765625", "-74.00137329101562" ); }; }And I can't tell if my problem is that $nearSphere is not getting floating numbers as values OR if there is something else wrong in my code.
Thanks.
2 Answers
Hi there,
max distance is expressed in degrees - I think that's the correct term. My technical geography is terrible - got no idea what that all means but I use this to find places in the map region displayed on screen. It's very crude but does an okay job
lnglat : { "$nearSphere" : [longitude, latitude], "$maxDistance" : (mapView.longitudeDelta + mapView.latitudeDelta) / 2, } }
I am going to answer my own question since I figured out the answer myself.
1) In the $nearSphere parameter I was passing [latitude,longitude] instead of [longitude,latitude].
2) $maxDistance is in decimal degrees.
I couldn't find a calculator that simply converts decimal degrees to miles but I figured out that 1 mile is approximately 0.000126 decimal degrees. Note that this is a really rough approximation and if you want exact values you should take into account that the earth is not a perfect sphere and calculate the distance based on where on the planet you are measuring this distance.
Your Answer
Think you can help? Login to answer this question!