Question

I have this search query in php:

[$near] => Array(
  [0] => -73.5539925
  [1] => 45.5086699
)
[$maxDistance] => 0.269978401728

0.269978401728 was calculated this way: 30/111.12

problem is that it doesn't find point with these values: -73.8260706 45.4293058 it finds it only if distance is increased to 32. If I check gmap walking distance between these points is 26.6 which is longer than direct line. Then even with 25km it should probably work, but now it doesnt work with 30. Do I miss something?

Thanks

Was it helpful?

Solution

I changed query to use within instead of near and now it works fine

$radiusOfEarth = 6378.1;//radius in km

$calculated_distance = (float) $distance  / $radiusOfEarth;
$search = array(
      'loc' => array('$within' =>
            array('$centerSphere' =>
                array(
                    array(floatval($lng), floatval($lat)), $calculated_distance
                )
            )
        )
  );

or

[loc] => Array
    (
        [$within] => Array
            (
                [$centerSphere] => Array
                    (
                        [0] => Array
                            (
                                [0] => -73.5539925
                                [1] => 45.5086699
                            )

                        [1] => 0.00501716812217
                    )

            )

    )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top