/** * Compute distance (in radians) to a geo point * * @param GeoPoint $point Other geo point * @return number */ public function radiansTo(GeoPoint $point) { $d2r = M_PI / 180.0; $lat1rad = $this->getLatitude() * $d2r; $lon1rad = $this->getLongitude() * $d2r; $lat2rad = $point->getLatitude() * $d2r; $lon2rad = $point->getLongitude() * $d2r; $deltaLat = $lat1rad - $lat2rad; $deltaLon = $lon1rad - $lon2rad; $sinLat = sin($deltaLat / 2); $sinLon = sin($deltaLon / 2); $a = $sinLat * $sinLat + cos($lat1rad) * cos($lat2rad) * $sinLon * $sinLon; $a = min(1.0, $a); return 2 * asin(sqrt($a)); }
/** * @covers GeoPoint::getLatitude */ public function testGetLatitude() { $this->assertSame($this->latDegrees, $this->instance->getLatitude()); $this->instance->setOutputFormat(GeoPoint::RADIANS); $this->assertSame($this->latRad, $this->instance->getLatitude()); }
public function addNearCondition($field, GeoPoint $point, $booleanOperator = 'AND') { if ($point->hasAllDistances()) { $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]], '$minDistance' => $point->getMinDistance(), '$maxDistance' => $point->getMaxDistance()]; } else { if ($point->hasMaxDistance()) { $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]], '$maxDistance' => $point->getMaxDistance()]; } else { if ($point->hasMinDistance()) { $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]], '$minDistance' => $point->getMinDistance()]; } else { $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]]]; } } } $this->addRawCondition($field, $prepared, $booleanOperator); }