GEO Postcode more countrys

49 Views Asked by At

It is about my site, it is a ad portal and 3 geodata are installed in the system: Germany, Switzerland and Austria.

When I look for an advertisement in Germany, everything works correctly, I'm looking for zip code 68259 and a radius of 30 km. The results are correct, it shows all ads from 68259 Mannheim and the radius of 30 km.

Problem: The problem exists when I search in Switzerland or Austria: I search for the postal code 6000 Lucerne 1 PF and a radius of 30 km ... the results are wrong, I also find ads from Munich or Frankfurt which correspond to 300-500 km radius! I think the mistake is somewhere here in the GEO Post code ! Any Help?

    $this->search_code    = htmlspecialchars($key);
    $this->search_radius  = intval($_GET['search-area']);

    // Germany Postcode
    preg_match('/\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b/is', $this->search_code, $output);
    
    if(!empty($output[0])){
        $this->search_code = $output[0];
    }else{
        // Switzerland, Austria Postcode
        preg_match('/\d{4}/', $this->search_code, $at_ch);
        if(!empty($at_ch[0])){
            $this->search_code = $at_ch[0];
        }
    }

    if( is_numeric($this->search_code) ){
        $this->dbValue = 'geo_plz'; }else{
        $this->dbValue = 'geo_ort'; }

    //query->build
    $get = $this->db->db->query("
        SELECT  geo_lg AS lon, geo_plz, geo_bg AS lat
        FROM    geo
        WHERE   ".$this->dbValue." = '".$this->search_code."'
        ");
    $row = $get->fetch();

    $lon = $row['lon'] / 180 * M_PI;
    $lat = $row['lat'] / 180 * M_PI;
    $plz = $row['geo_plz'];

    //radius->query->search
    $query_boot = "SELECT geo_plz AS plz, (".$this->earthRadius." * SQRT(2*(1-cos(RADIANS(geo_bg)) *
             cos(".$lat.") * (sin(RADIANS(geo_lg)) *
             sin(".$lon.") + cos(RADIANS(geo_lg)) *
             cos(".$lon.")) - sin(RADIANS(geo_bg)) * sin(".$lat.")))) AS Distance
             FROM geo WHERE
             ".$this->earthRadius." * SQRT(2*(1-cos(RADIANS(geo_bg)) *
             cos(".$lat.") * (sin(RADIANS(geo_lg)) *
             sin(".$lon.") + cos(RADIANS(geo_lg)) *
             cos(".$lon.")) - sin(RADIANS(geo_bg)) * sin(".$lat."))) <= ".$this->search_radius."
             ORDER BY Distance
            ";

    $getString = $this->db->db->query($query_boot);
    $rowString = $getString->fetchAll();

    foreach( $rowString as $key => $value ){
        $dataPackageString[] = $value['plz'];
    }

    $resultListener = "";
    if(!empty($dataPackageString)) {
        $listener = join(',', $dataPackageString);
        $listener = str_replace(',', '|', $listener);

        $resultListener = "AND ( postleitzahl REGEXP '$listener|$plz' OR stadt LIKE '%" . $this->search_code . "%' )";
    }else{
        $resultListener = "AND ( postleitzahl = '".$plz."' OR stadt LIKE '%".$this->search_code."%' )";
    }

    return $resultListener;
0

There are 0 best solutions below