123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/3/7
- * Time: 下午2:46
- * amap 是调用高德地图接口,baidu 是调用百度接口
- */
- require_once(BASE_ROOT_PATH . '/core/framework/function/http.php');
- class amap
- {
- public function call_map($city,$address)
- {
- $key = 'a284df4b8cef6aa5144f43ad5753b7d4'; //高德地图访问key,账号:13911129867,密码:jyc2018168.
- $url = "http://restapi.amap.com/v3/geocode/geo";
- $params = ['key' => $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;
- }
- }
|