Created
September 15, 2016 14:54
-
-
Save goofyahead/c0b75ca3f8515ff0b046aec1f9a60933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// proximity using (slightly) limited native.geoNear | |
geoProximity: function (req,res) { | |
console.log('MeetingsController: action=geoProximity '); | |
console.log(' req.isSocket ', req.isSocket); | |
console.log(' req.isAjax ', req.isAjax); | |
console.log(' req.isJson ', req.isJson); | |
var lat = parseFloat(req.param('lat')); | |
var lng = parseFloat(req.param('lng')); | |
var maxDistance = req.param('maxDistance') || 1000; | |
var limit = req.param('limit') || 30; | |
console.log(' lat ', lat, typeof lat); | |
console.log(' lng ', lng); | |
console.log(' maxDistance ', maxDistance); | |
console.log(' limit ', limit); | |
Meetings.native(function(err, collection) { | |
/* | |
* geoNear function defined in node-mongodb-native (see https://github.com/mongodb/node-mongodb-native/blob/dd7bb687749ffab6ec4c4a6b052ef2cdffc0d780/lib/mongodb/collection.js#L1446) | |
* also see MongoDoc http://docs.mongodb.org/manual/reference/command/geoNear/ | |
* | |
* Execute the geoNear command to search for items in the collection | |
* | |
* Options | |
* - **num** {Number}, max number of results to return. | |
* - **maxDistance** {Number}, include results up to maxDistance from the point. | |
* - **distanceMultiplier** {Number}, include a value to multiply the distances with allowing for range conversions. | |
* - **query** {Object}, filter the results by a query. | |
* - **spherical** {Boolean, default:false}, perform query using a spherical model. | |
* - **uniqueDocs** {Boolean, default:false}, the closest location in a document to the center of the search region will always be returned MongoDB > 2.X. | |
* - **includeLocs** {Boolean, default:false}, include the location data fields in the top level of the results MongoDB > 2.X. | |
* - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST). | |
* | |
* @param {Number} x point to search on the x axis, ensure the indexes are ordered in the same order. | |
* @param {Number} y point to search on the y axis, ensure the indexes are ordered in the same order. | |
* @param {Objects} [options] options for the map reduce job. | |
* @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the geoNear method or null if an error occured. | |
* @return {null} | |
* @api public | |
*/ | |
collection.geoNear(lng, lat, { | |
limit: limit, | |
maxDistance: maxDistance, // in meters | |
//query: {}, // allows filtering | |
distanceMultiplier: 3959, // converts radians to miles (use 6371 for km) | |
spherical : true | |
}, function(mongoErr, docs) { | |
if (mongoErr) { | |
console.error(mongoErr); | |
res.send('geoProximity failed with error='+mongoErr); | |
} else { | |
console.log('docs=',docs); | |
// res.send('proximity successful, got '+docs.results.length+' results.'); | |
res.json(docs.results); | |
} | |
}); | |
}); | |
}, | |
// within using native and find($geoWithin) | |
geoWithin: function (req,res) { | |
console.log('MeetingsController: action=geoWithin '); | |
var north = parseFloat(req.param('north')); | |
var west = parseFloat(req.param('west')); | |
var south = parseFloat(req.param('south')); | |
var east = parseFloat(req.param('east')); | |
var limit = req.param('limit') || 30; | |
console.log(' north ', north); | |
console.log(' west ', west); | |
console.log(' south ', south); | |
console.log(' east ', east); | |
console.log(' limit ', limit); | |
Meetings.native(function(err, collection) { | |
collection.find({ | |
'location.center': { | |
$geoWithin: { // see http://docs.mongodb.org/manual/reference/operator/geoWithin/#op._S_geoWithin | |
$geometry: { | |
type: 'Polygon', | |
coordinates: [ // use format [lng, lat] | |
[ [ west, north ], [ east, north ], [ east, south ], [ west, south ], [ west, north ] ] | |
] | |
} | |
} | |
} | |
}) | |
.toArray(function(mongoErr, docs) { | |
if (mongoErr) { | |
console.error(mongoErr); | |
res.send('geoWithin failed with error='+mongoErr); | |
} else { | |
console.log('docs.length=',docs.length); | |
// res.send('within successful, got '+docs.results.length+' results.'); | |
res.json(docs); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment