Welcome Guest, Not a member yet? Register   Sign In
get users who live within 10 km radius of the current logged in user codeigniter 3
#1

(This post was last modified: 10-05-2019, 06:28 PM by tp45.)

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


Messages In This Thread
get users who live within 10 km radius of the current logged in user codeigniter 3 - by tp45 - 10-05-2019, 04:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB