$key,'address' => $address,'city' => $city]; $resp = http_request($url,$params); $data = json_decode($resp,true); if($data['status'] != 1) { return false; } else { return $data; } } public function geocodes($city,$address) { $resp = $this->call_map($city,$address); if($resp === false) return false; $geocodes = $resp['geocodes']; if(count($geocodes) <= 0 || count($geocodes) > 1) { return false; } else { $location = $geocodes[0]['location']; $lng_lat = mb_split(',',$location); return $lng_lat; } } } class baidu { static $key = '5MINsIU8ukWmTn48t9fcGpEUafoQF8Kg'; static $url = 'http://api.map.baidu.com/geocoder/v2/'; public function call_map($city,$address) { //http://api.map.baidu.com/geocoder/v2/?address=&output=json&ak=&callback=showLocation $params = ['ak' => self::$key,'address' => "{$city}{$address}",'output' => 'json', 'coordtype' => 'bd09ll' ]; $resp = http_request(self::$url, $params); $data = json_decode($resp,true); if($data['status'] != 0) { return false; } else { return $data; } } public function geocodes($city,$address) { $resp = $this->call_map($city,$address); if($resp === false) return false; $loc = $resp['result']['location']; return [ $loc['lng'],$loc['lat'] ]; } public function getReverse($location) { $params = [ 'ak' => self::$key, 'location' => $location, 'output' => 'json', 'coordtype' => 'bd09ll', 'pois' => 0 ]; $resp = http_request(self::$url, $params); $data = json_decode($resp,true); if($data['status'] != 0) { return false; } else { return $data; } } } class geo_helper { static private $stInstance = null; private function __construct() { } static public function instance() { if (self::$stInstance == null) { self::$stInstance = new geo_helper(); } return self::$stInstance; } public function geocodes($city,$address) { $amap = new amap(); return $amap->geocodes($city,$address); } public function getdistance($lng1,$lat1,$lng2,$lat2) { $fEARTH_RADIUS = 6378137; $radLat1 = deg2rad($lat1); $radLat2 = deg2rad($lat2); $radLng1 = deg2rad($lng1); $radLng2 = deg2rad($lng2); $a = abs($radLat1-$radLat2); $b = abs($radLng1-$radLng2); $fP = pow(sin($a/2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2), 2); return intval($fEARTH_RADIUS * 2 * asin(sqrt($fP)) + 0.5); } public function update_store($store_id) { $model_store = Model('store'); $store_info = $model_store->getStoreInfo(['store_id' => $store_id]); if(empty($store_info)) return false; $area_info = $store_info['area_info']; if(empty($area_info)) return false; $regxp = '/(\S+)[\s]*(\S+)*/u'; $val = preg_match_all($regxp,$area_info,$match); if($val <= 0 || $val == false) return false; if(count($match) != 3) return false; if(empty($match[2][0])) { $city = $match[1][0]; } else { $city = $match[2][0]; } $cityids = $this->cityids($city); if(empty($cityids)) return false; $city_id = $cityids['city_id']; $prov_id = $cityids['prov_id']; if(empty($store_info['store_address'])) return false; $lng_lat = $this->geocodes($city,$store_info['store_address']); if($lng_lat != false) { $store_latlng = implode(',',$lng_lat); return $model_store->editStore(['province_id' => $prov_id,"city_id" => $city_id,"store_latlng" => $store_latlng],['store_id' => $store_id]); } else { return false; } } static public function asc_dis($left,$right) { $t_l = intval($left['distance']); $t_r = intval($right['distance']); if($t_l > $t_r) return 1; elseif($t_l < $t_r) return -1; else return 0; } public function asc_stores($car_stores,$usrloc,$city) { if(empty($car_stores)) return []; $cityids = $this->cityids($city); if($cityids !== false) { $cityid = $cityids['city_id']; } else { $cityid = 0; } $car_stores = $this->store_infos($car_stores,$cityid); $local_stores = $car_stores['local_city']; $other_stores = $car_stores['other_city']; if(!empty($local_stores)) { foreach ($local_stores as &$item) { $s_loc = explode(',',$item['store_latlng']); $dis = $this->getdistance($usrloc[0],$usrloc[1],$s_loc[0],$s_loc[1]); $item['distance'] = $dis; } uasort($local_stores,['geo_helper','asc_dis']); $match_stores = $local_stores; } else { foreach ($other_stores as &$item) { $s_loc = explode(',',$item['store_latlng']); $dis = $this->getdistance($usrloc[0],$usrloc[1],$s_loc[0],$s_loc[1]); $item['distance'] = $dis; } usort($other_stores,['geo_helper','asc_dis']); $match_stores = $other_stores; } return $match_stores; } private function store_infos($stores,$cityid) { $store_ids = []; foreach ($stores as $store) { $store_ids[] = intval($store); } $mod_store = Model('store'); $stores = $mod_store-> getStoreList(['store_id' => ['in',$store_ids]]); $result = []; $result['local_city'] = []; $result['other_city'] = []; if($cityid > 0) { foreach ($stores as $store) { if($cityid == intval($store['city_id'])) { $result['local_city'][] = $store; } else { $result['other_city'][] = $store; } } } else { $result['other_city'] = $stores; } return $result; } public function cityids($city) { if(empty($city)) return false; $mod_area = Model('area'); $info = $mod_area->getAreaInfo(['area_name' => $city]); if(empty($info)) return false; $parent_id = intval($info['area_parent_id']); if($parent_id === 0) { $ret['city_id'] = 0; $ret['prov_id'] = $info['area_id']; } else { $ret['city_id'] = $info['area_id']; $ret['prov_id'] = $info['area_parent_id']; } return $ret; } public function city_info($city_id) { $city_id = intval($city_id); if($city_id <= 0) return false; $mod_area = Model('area'); $info = $mod_area->getAreaInfo(['area_id' => $city_id]); if(empty($info)) return false; $parent_id = intval($info['area_parent_id']); if($parent_id === 0) { $ret['city_id'] = 0; $ret['prov_id'] = $info['area_id']; $ret['prov_name'] = $info['area_name']; $ret['city_name'] = ''; } else { $ret['city_id'] = $info['area_id']; $ret['prov_id'] = $parent_id; $ret['city_name'] = $info['area_name']; $prov = $mod_area->getAreaInfo(['area_id' => $parent_id]); $ret['prov_name'] = $prov['area_name']; } return $ret; } }