php mysql compare long and lat, return ones under 10 miles

前端 未结 6 1684
借酒劲吻你
借酒劲吻你 2020-12-15 00:35

Hay i want to find the distance (in miles) between 2 locations using lat and long values, and check if they are within a 10 mile radius of each other.

When a user lo

6条回答
  •  臣服心动
    2020-12-15 01:25

    Searching in KM: ($LATITUDE -> Your latitude, $LONGITUDE -> Your longitude, latitud_fieldname -> Your latitude database fieldname, longitude_fieldname -> Your longitude database fieldname)

    SELECT * FROM (
        SELECT *, 
            (
                (
                    (
                        acos(
                            sin(( $LATITUDE * pi() / 180))
                            *
                            sin(( `latitud_fieldname` * pi() / 180)) + cos(( $LATITUDE * pi() /180 ))
                            *
                            cos(( `latitud_fieldname` * pi() / 180)) * cos((( $LONGITUDE - `longitude_fieldname`) * pi()/180)))
                    ) * 180/pi()
                ) * 60 * 1.1515 * 1.609344
            )
        as distance FROM `myTable`
    ) myTable
    WHERE distance <= $DISTANCE_KILOMETERS;
    

    Searching in Miles:

    SELECT * FROM (
        SELECT *, 
            (
                (
                    (
                        acos(
                            sin(( $LATITUDE * pi() / 180))
                            *
                            sin(( `latitud_fieldname` * pi() / 180)) + cos(( $LATITUDE * pi() /180 ))
                            *
                            cos(( `latitud_fieldname` * pi() / 180)) * cos((( $LONGITUDE - `longitude_fieldname`) * pi()/180)))
                    ) * 180/pi()
                ) * 60 * 1.1515
            )
        as distance FROM `myTable`
    ) myTable
    WHERE distance <= $DISTANCE_MILES;
    

    For full info, follow the link: https://ourcodeworld.com/articles/read/1019/how-to-find-nearest-locations-from-a-collection-of-coordinates-latitude-and-longitude-with-php-mysql

提交回复
热议问题