• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jul 23rd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. <?php
2.
3. namespace App\Services\Coordinates;
4.
5. class CoordinateService
6. {
7.     /**
8.      * Computes the distance between two coordinates.
9.      *
10.      * Implementation based on reverse engineering of
11.      * <code>google.maps.geometry.spherical.computeDistanceBetween()</code>.
12.      *
13.      * @param float \$lat1   latitude from the first point
14.      * @param float \$lng1   longitude from the first point
15.      * @param float \$lat2   latitude from the second point
16.      * @param float \$lng2   longitude from the second point
17.      * @param float \$radius (optional) Radius in meters
18.      *
19.      * @return float distance in meters
20.      */
21.     public function computeDistance(\$lat1, \$lng1, \$lat2, \$lng2, \$radius = 6378137)
22.     {
23.         static \$x = M_PI / 180;
24.         \$lat1 *= \$x;
25.         \$lng1 *= \$x;
26.         \$lat2 *= \$x;
27.         \$lng2 *= \$x;
28.         \$distance = 2 * asin(sqrt(pow(sin((\$lat1 - \$lat2) / 2), 2) + cos(\$lat1)
29.         * cos(\$lat2) * pow(sin((\$lng1 - \$lng2) / 2), 2)));
30.
31.         return \$distance * \$radius;
32.     }
33.
34.     /*
35.      * Algorithm based on the spherical law of cosines.
36.      * https://developers.google.com/maps/solutions/store-locator/clothing-store-locator#creating-the-map
37.      *
38.      *To count in miles change 6371 to 3959
39.      */
40.     public function pointsInRadiusQuery(array \$data)
41.     {
42.         return '*,
43.                (
44.                    6371 *
45.                    acos(cos(radians('.\$data['latitude'].')) *
46.                    cos(radians(`latitude`)) *
47.                    cos(radians(`longitude`) -
48.                    radians('.\$data['longitude'].')) +
49.                    sin(radians('.\$data['latitude'].')) *
50.                    sin(radians(`latitude` )))
51.                )
52.                AS distance';
53.     }
54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top