• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get users who live within 10 km radius of the current logged in user codeigniter 3

#1
Hello
am trying to get all user who are within 10 km radius of the current logged in user so i tried getting only the addresses i get them but now i want to link the addresses with their owners so that i can display the users names etc.
so to get the addresses i used this code:
PHP Code:
    function Addresses($CurrentUserAdd){
        
$CI =& get_instance();
    $query $CI->db->get('users');
    $results = array();
    while ($row $query->unbuffered_row('array')) {
        if (getDistance($CurrentUserAdd$row['address']) <= 10.00) {
            $results[] = getDistance($CurrentUserAdd$row['address']);
        }
    }
    return $results;
    }
    
    function 
getDistance($CurrentUserAddress$otherUserAddress$unit 'k'){
    
    // Google API key
    
    $apiKey 'AIzaSyBRMLF9pK8EoY-wOnp1_N1uZ7pH6fOnlLQ';
    
    
        
// Change address format
    
    $formattedAddrFrom    str_replace(' ''+'$CurrentUserAddress);
    
    $formattedAddrTo     str_replace(' ''+'$otherUserAddress);
    
    
        
// Geocoding API request with start address
    
    $geocodeFrom file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false&key='.$apiKey);
    
    $outputFrom json_decode($geocodeFrom);
    
    if(!empty($outputFrom->error_message)){
    
        return $outputFrom->error_message;
    
    }
    
    
        
// Geocoding API request with end address
    
    $geocodeTo file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false&key='.$apiKey);
    
    $outputTo json_decode($geocodeTo);
    
    if(!empty($outputTo->error_message)){
    
        return $outputTo->error_message;
    
    }
    
    
        
// Get latitude and longitude from the geodata
    
    $latitudeFrom    $outputFrom->results[0]->geometry->location->lat;
    
    $longitudeFrom    $outputFrom->results[0]->geometry->location->lng;
    
    $latitudeTo        $outputTo->results[0]->geometry->location->lat;
    
    $longitudeTo    $outputTo->results[0]->geometry->location->lng;
    
    
        
// Calculate distance between latitude and longitude
    
    $theta    $longitudeFrom $longitudeTo;
    
    $dist    sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
    
    $dist    acos($dist);
    
    $dist    rad2deg($dist);
    
    $miles    $dist 60 1.1515;
    
    
        
// Convert unit and return distance
    
    $unit strtoupper($unit);
    
    if($unit == "K"){
    
        return round($miles 1.6093442);
    
    }elseif($unit == "M"){
    
        return round($miles 1609.3442);
    
    }else{
    
        return round($miles2);
    
    }
    } 
My view: to view the km less than 10.00km
PHP Code:
print_r(Addresses($this->session->userdata('address')));
i get this 

Array 
PHP Code:

PHP Code:
    [0] => 1.31 
PHP Code:
    [1] => 1.85 
PHP Code:
    [2] => 3.15 
PHP Code:
    [3] => 
PHP Code:
    [4] => 1.84 
PHP Code:
    [5] => 1.54 
PHP Code:

i tried this to get all users data with less than 10 km but it just gives me all users 

PHP Code:
    function Addresses($CurrentUserAdd){
        
$CI =& get_instance();
    $CI->db->where(getDistance($CurrentUserAdd'address') <= 10.00);
    $query $CI->db->get('users');
    return $query->result_array();
    } 
i just want to recommend users closer to the currently logged in user
Please help thanks in advance.
Am using mssql server so i saw STDistance function but i dont know if this is doable for many users
Reply

#2
First, get all "other" users from your database, i.e. all users where id is not the id of the current user. Get the results in an array named $users.
Also create a new empty array, named $nearby_Users.
Then, loop through all rows in the users array. Calculate the distance between the user's location and the location of the "current user", with your function getDistance().
If the distance is 10 km or less, add that user's data to the $nearby_Users array.
After the loop, do whatever you need with the $nearby_Users array.

There's no need to make a complicated SQL query to get the final result.
Reply

#3
(10-06-2019, 09:56 AM)Wouter60 Wrote: First, get all "other" users from your database, i.e. all users where id is not the id of the current user. Get the results in an array named $users.
Also create a new empty array, named $nearby_Users.
Then, loop through all rows in the users array. Calculate the distance between the user's location and the location of the "current user", with your function getDistance().
If the distance is 10 km or less, add that user's data to the $nearby_Users array.
After the loop, do whatever you need with the $nearby_Users array.

There's no need to make a complicated SQL query to get the final result.
Thank you @Wouter60. let me impliment that
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.