0

Struggling to calculate radians of a circle use mongoose/mongodb geoWithin query. I have the user's location, and I the result furthest away from that user. From those two points I need to calculate radians so I can search in a full circle around that user reaching out to furthest result. Hopefully that makes sense. :)

    //get closest item to user (just because)
    var closestLon = vendorPosts[0].location.long;
    var closestLat = vendorPosts[0].location.lat;

    //get furthest item from user
    var furthestLon = vendorPosts[vendorPosts.length - 1].location.long;
    var furthestLat = vendorPosts[vendorPosts.length - 1].location.lat;

    //User is located here
    var userLat = req.query.lat;
    var userLon = req.query.long;

    //Number of kilometers furthest item is from user
    var maxDistanceKm = getDistanceFromLatLonInKm(userLat, userLon, furthestLat, furthestLon);

    //Attempting to convert to rads but results never appear, is this correct calculation?
    var maxDistanceRadians = maxDistanceKm/6371;

    Model.find({
        $text: { $search: searchterm, $language: 'english' },
        coordinates :
             { $geoWithin :
                 {
                     $center : [ [ userLon, userLat ] , maxDistanceRadians ]
                 }
             }
        } , { score: {
                $meta: "textScore"
            }
        }, function (err, results) {
            ....
        });

Using these famous haversine formulas/utils

    function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
        var R = 6371; // Radius of the earth in miles
        var dLat = deg2rad(lat2-lat1);  // deg2rad below
        var dLon = deg2rad(lon2-lon1);
        var a =
              Math.sin(dLat/2) * Math.sin(dLat/2) +
              Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
              Math.sin(dLon/2) * Math.sin(dLon/2)
            ;
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c; // Distance in km
        return d;
    }

    function deg2rad(deg) {
        return deg * (Math.PI/180)
    }
That1guyoverthr
  • 1,113
  • 2
  • 12
  • 19
  • The formula looks correct, you can see an implementation here to check. http://stackoverflow.com/questions/14560999/using-the-haversine-formula-in-javascript. What issues are you having exactly? – John Powell Sep 18 '14 at 15:08

1 Answers1

0

Look like I drastically overcomplicated my answer. I already had the center point of a circle (user location) and I already had the item furthest away from the user. From these two points I needed to calculate radius and plug into mongo.

    var furthestitem = vendorPosts[vendorPosts.length - 1];

    var radius = Math.sqrt( Math.pow((userLat-furthestitem.location.lat), 2) + Math.pow((userLong-furthestitem.location.long), 2) )

    PostModel.find({
        $text: { $search: heading, $language: 'english' },
        $or: mongoCategories,
        coordinates :
             { $geoWithin :
                 {
                     $center : [ [ userLong, userLat ] , radius ]
                 }
             }
        } , { score: {
                $meta: "textScore"
            }
        }, function (err, results) {  ...... });
That1guyoverthr
  • 1,113
  • 2
  • 12
  • 19