javascript - MeteorJS: Display Posts with locations within x miles of current location -


i working on application display posts of people within amount of distance of location.

whenever post created value of location created stored with: {"lat":39.7230949,"lng":-104.83521619999999}. lat , longitude of current location when post made.

this function, geolocation.latlng();, prints out object lat , lng of current device.

i trying figure out how have page lists post, show post fall within radius of devices current location.

here current function lists post database.

template.yakresults.searchresults = function () {     var keyword  = session.get("search-query");     var query = new regexp( keyword, 'i' );     var results = yaks.find( { $or: [{'yak': query}] }, {sort: {score: -1}});     return {results: results}; } 

what have results 10 mile radius of devices current long/lat can found function mentioned above.

i imagine use if statement of sort or maybe filer when finding posts in yaks collection?

does 1 have idea on how can filter collection of posts similar this?

if need post more code make easier understand question please let me know , will.

figured out. 1 may trying similar in future, here did.

first had have posts location formatted geojson before go database. here code used in order achieve this.(note: gelocation.latlng(); function meteor plugin(https://atmospherejs.com/mdg/geolocation)).

this code called whenever yak submitted.

template.yakssubmit.events({ 'submit .yakssubmitform': function(event) {          event.preventdefault();         var yak = event.target.yak.value; //get yak input value         var yaklocation = geolocation.latlng(); // location of yak          var lat = yaklocation.lat;          var lng = yaklocation.lng;          //check if value empty         if(yak == "") {             alert("you can't inert empty yak!");         } else {             meteor.call('yakinsert', yak, lng, lat);             router.go('yakslist');         }     } }); 

here yakinster method being called server.js file.

yakinsert: function(yak, lng, lat) {     var postid = yaks.insert({         yak: yak,         loc : {             type: "point",             coordinates: [ lng, lat ]         },         score: 0,         submitted: new date(),         user: meteor.userid(),         lng: lng,         lat: lat         // geolocation.latlng(); json.stringify(yaklocation)     }); } 

then stored devices long/lat in session helper.

template.yakresults.helpers({ lat: function() {         var currentlocation = geolocation.latlng(); // location of yak         var lat = currentlocation.lat;         session.set("device-lat", lat);     },     lng: function() {         var currentlocation = geolocation.latlng(); // location of yak         var lng = currentlocation.lng;         session.set("device-lng", lng);     } }); 

and function display posts used information explained in blogpost on mongolab(http://blog.mongolab.com/2014/08/a-primer-on-geospatial-data-and-mongodb/). here function looks like

template.yakresults.searchresults = function () {     var lng = session.get("device-lng");     var lat = session.get("device-lat");     var query = new regexp( keyword, 'i' );     var results = yaks.find( {      loc:        { $near :           {             $geometry:{ type:"point", coordinates:[ lng, lat]},             $mindistance: 0,             $maxdistance: 5000           }        }    });     return {results: results}; } 

this got posts display order determined location of each yak/post relative device's location.

the $mindistance & $maxdistance in meters.

this might not best way make work worked me. thought share incase else use it.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -