Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. /* Place model */
  2. module.exports = {
  3.  
  4. attributes: {
  5.  
  6. name: {
  7. type: 'string',
  8. required: true
  9. },
  10.  
  11. // This is the attribute used for the place's geolocation.
  12. // Be careful to store it as [ lng, lat ] or else geoNear queries will give imprecise results.
  13. coordinates: {
  14. type: 'json',
  15. required: true
  16. }
  17.  
  18. },
  19.  
  20. /**
  21. * Find places closer than a certain distance (in km) from a specified location [ lng, lat ].
  22. * @param conditions
  23. * JSON object should look like this:
  24. * {
  25. * lng: -72.213,
  26. * lat: 45.012,
  27. * maxDistance: 100,
  28. * limit: 20,
  29. * }
  30. *
  31. * @param callback (err, results)
  32. * Returns an array of results (ordered by increasing distance), it looks like this:
  33. * [
  34. * {
  35. * dis: 10.321,
  36. * obj: { JSON object of Place }
  37. * },
  38. * {
  39. * dis: 20.123,
  40. * obj: { JSON object of Place }
  41. * }
  42. * ]
  43. */
  44. findNear: function (conditions, callback) {
  45.  
  46. Place.native(function (err, collection) {
  47. if (err) return callback(err);
  48.  
  49. collection.geoNear({
  50. type: "Point" ,
  51. coordinates: [ conditions.lng, conditions.lat ]
  52. }, {
  53. limit: conditions.limit || 30,
  54. maxDistance: conditions.maxDistance * 1000.0,
  55. distanceMultiplier: 0.001,
  56. spherical : true
  57. }, function (err, places) {
  58. if (err) return callback(err);
  59.  
  60. return callback(null, places.results);
  61. });
  62. });
  63.  
  64. }
  65.  
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement