Storing user's location in DB and find the nearest users for him

前端 未结 1 1153
不知归路
不知归路 2020-12-20 03:54

I am trying to implement a user system, one of the system requirements is find other users that are near you.

Location will be filtered by countries but not by citie

相关标签:
1条回答
  • 2020-12-20 04:48

    You can use the Haversine formula to search for nearest user in a database.

    $stmt = $dbh->prepare("SELECT  name, lat, lng, ( 3959 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance FROM mytable HAVING distance < ? ORDER BY distance LIMIT 0 , 20");
        // Assign parameters
        $stmt->bindParam(1,$center_lat);//lat coordinate of user
        $stmt->bindParam(2,$center_lng);//lng coordinate of user
        $stmt->bindParam(3,$center_lat);
        $stmt->bindParam(4,$radius);//Radius in miles 
    

    A database of cities can be found at Geonames

    0 讨论(0)
提交回复
热议问题