Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services\Coordinates;
- class CoordinateService
- {
- /**
- * Computes the distance between two coordinates.
- *
- * Implementation based on reverse engineering of
- * <code>google.maps.geometry.spherical.computeDistanceBetween()</code>.
- *
- * @param float $lat1 latitude from the first point
- * @param float $lng1 longitude from the first point
- * @param float $lat2 latitude from the second point
- * @param float $lng2 longitude from the second point
- * @param float $radius (optional) Radius in meters
- *
- * @return float distance in meters
- */
- public function computeDistance($lat1, $lng1, $lat2, $lng2, $radius = 6378137)
- {
- static $x = M_PI / 180;
- $lat1 *= $x;
- $lng1 *= $x;
- $lat2 *= $x;
- $lng2 *= $x;
- $distance = 2 * asin(sqrt(pow(sin(($lat1 - $lat2) / 2), 2) + cos($lat1)
- * cos($lat2) * pow(sin(($lng1 - $lng2) / 2), 2)));
- return $distance * $radius;
- }
- /*
- * Algorithm based on the spherical law of cosines.
- * https://developers.google.com/maps/solutions/store-locator/clothing-store-locator#creating-the-map
- *
- *To count in miles change 6371 to 3959
- */
- public function pointsInRadiusQuery(array $data)
- {
- return '*,
- (
- 6371 *
- acos(cos(radians('.$data['latitude'].')) *
- cos(radians(`latitude`)) *
- cos(radians(`longitude`) -
- radians('.$data['longitude'].')) +
- sin(radians('.$data['latitude'].')) *
- sin(radians(`latitude` )))
- )
- AS distance';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement