Пример #1
0
 private function getCommList($brokerId, $lat, $lng)
 {
     // 从缓存中读取数据
     $communities = Bll_Commsign::getCommList($brokerId);
     if (empty($communities)) {
         // 根据lat和lng获取1.5km内的小区列表
         $latLngRound = Util_Map::round($lat, $lng, 1500);
         $communities = Model_Community_AjkCommunity::data_access()->load_field(array('commId', 'commName', 'sosolng', 'sosolat'))->filter('typeFlag', 0)->filter_by_op_multi(array(array('sosolat', '>=', $latLngRound['minLat']), array('sosolat', '<=', $latLngRound['maxLat']), array('sosolng', '>=', $latLngRound['minLng']), array('sosolng', '<=', $latLngRound['maxLng'])))->limit(1000)->get_all();
         if (empty($communities)) {
             return array();
         }
         // 实现按距离排序 • 升序
         uasort($communities, function (&$a, &$b) use($lat, $lng) {
             // 计算坐标的距离
             $a['distance'] = Util_Map::distance($lat, $lng, $a['sosolat'], $a['sosolng']);
             $b['distance'] = Util_Map::distance($lat, $lng, $b['sosolat'], $b['sosolng']);
             // 比较两小区坐标
             if ($a['distance'] == $b['distance']) {
                 return 0;
             }
             return $a['distance'] < $b['distance'] ? -1 : 1;
         });
         $i = 0;
         $data = array();
         foreach ($communities as $community) {
             if (++$i > 300) {
                 break;
             }
             $row = array();
             $row['commId'] = $community['commId'];
             $row['commName'] = $community['commName'];
             $row['lng'] = $community['sosolng'];
             $row['lat'] = $community['sosolat'];
             $row['distance'] = $community['distance'];
             $data[] = $row;
         }
         // 缓存数据
         Bll_Commsign::setCommList($brokerId, $data);
         return $data;
     } else {
         if (is_array($communities)) {
             return $communities;
         } else {
             return array();
         }
     }
 }