Last active
October 8, 2015 19:18
-
-
Save notomato/3377689 to your computer and use it in GitHub Desktop.
Working with geolocation
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
<?php | |
$coords = Geocoder::find('google', "Sydney, Australia"); | |
// Within a box | |
$files = Files::find('within', [ | |
'conditions' => [ | |
'location' => [ | |
'$within' => [ | |
'$box' => [ | |
[-33.9984845, 150.8660859], | |
[-33.7535622, 151.2861399] | |
] | |
] | |
] | |
] | |
]); | |
$expected = 1; | |
$result = count($files); | |
$this->assertEqual($expected, $result); | |
// Within a circle, with a radius of 1 of the center point | |
$files = Files::find('within', [ | |
'conditions' => [ | |
'location' => [ | |
'$within' => [ | |
'$center' => [ | |
$coords, 1 | |
] | |
] | |
] | |
] | |
]; | |
$expected = 1; | |
$result = count($files); | |
$this->assertEqual($expected, $result); | |
// Within a circle, with a radius from of the center point | |
$files = Files::find('within', [ | |
'conditions' => [ | |
'location' => [ | |
'$within' => [ | |
'$center' => [ | |
$coords, 3 | |
] | |
] | |
] | |
] | |
]); | |
$expected = 2; | |
$result = count($files); | |
$this->assertEqual($expected, $result); | |
// Within a polygon | |
$polygon = [ | |
[145.371094, -32.517174], | |
[153.105469, -26.399743], | |
[159.082031, -32.368827], | |
[153.808594, -38.106899], | |
[144.84375, -32.960743], | |
[145.371094, -32.517174] | |
]; | |
$files = Files::find('within', array('conditions' => array('location' => array('$within' => array('$polygon' => $polygon)))))->to('array'); | |
// Near a point, returns closest number of points (use limit to restrict number). Closest outward. | |
$conditions = array('$near' => $coords); | |
$files = Files::find('near', array('conditions' => array('location' => $conditions)))->to('array'); | |
$expected = 3; | |
$result = count($files); | |
$this->assertEqual($expected, $result); | |
// Near a point, with maxDistance (radians). closest outward. | |
$conditions = array('$near' => $coords, '$maxDistance' => 1); | |
$files = Files::find('near', array('conditions' => array('location' => $conditions)))->to('array'); | |
$expected = 1; | |
$result = count($files); | |
$this->assertEqual($expected, $result); | |
// geoNear, this will also return the distance from the center point. Returns closest outward. | |
$files = Files::connection()->connection->command(array("geoNear" => "fs.files", "near" => $coords, "maxDistance" => 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment