Nathan Anyonge
Posts: 0
Joined: Thu Feb 26, 2015 3:05 pm

Geospatial Query

We have implemented a search query to display database content 40 metres from users location based on this java script:

params.criteria = {
"location" : {
"$nearSphere" : [48.208, 16.373],
"$maxDistanceInMiles" : 80
}
};

We would like the query to pull content based on the users current location, we store the users current location in a storage variables " userLng" and "userLat".
This is what what we changed it to:

params.criteria = {
"location" : {
"$nearSphere" : [localStorage.getItem("userLat"), localStorage.getItem("userLng")],
"$maxDistanceInKilometers" : 0.4
}
};

However its is still not working, any advice as to the proper script to pull information from the storage variables "userLat" and "userLng" would be much appreciated?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Geospatial Query

Hi Nathan,

You need to:

  1. add to your service (which is server script) some request parameters. Like "lat" and "lng". (client side)

  2. pass your storage values to these "lat" and "lng" service request parameters. (client side)

  3. then you can get these values in server code (server code side).

    Regards.

Asmaa AlHammadi
Posts: 0
Joined: Tue May 12, 2015 12:56 pm

Geospatial Query

can you please specify the steps? cause I really need to understand it

thank you,

Asmaa AlHammadi
Posts: 0
Joined: Tue May 12, 2015 12:56 pm

Geospatial Query

like what is the service code that need to be used

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Geospatial Query

Hi Asmaa,

Here is a JS code for server code that shows you how to work with geoSpatial requests:

precode

var dbId = "52fd3d0xxxxxx11c89917&quot // commented out
//You should use here your collection name.
var collectionName = "dogs&quot

var params = {};

params.criteria = {
"location": {
"$nearSphere": [48,40],
"$maxDistanceInMiles": 80
}
};

query = Collection.query(dbId, collectionName, params);

response.success(query, "application/json");

/code/pre

Regards.

Return to “Issues”