12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace baidu;
- /*
- 百度地图接口插件
- */
- class Map
- {
- private $AK="";
- public function __construct()
- {
- //执行父类的构造方法
- $this->AK = sysconfig('baidu.map_ak');
- }
- //wgs84转百度坐标
- public function wgs84_to_baidu($location)
- {
- $url='https://api.map.baidu.com/geoconv/v1/?coords='.$location['lng'].','.$location['lat'].'&from=1&to=5&ak='.$this->AK;
- $result=json_decode(file_get_contents($url),true);
- $result = $result['result'][0];
- if(isset($result['x'])){
- $loc['lng']=$result['x'];
- $loc['lat']=$result['y'];
- return $loc;
- }else{
- return false;
- }
- }
- //地址转经纬度
- public function address_to_location($Address)
- {
- $Address=str_replace(' ','',$Address);
- $url='https://api.map.baidu.com/geocoding/v3/?address='.$Address.'&city=宜昌市&output=json&ak='.$this->AK;
- $result=json_decode(file_get_contents($url),true);
- if(isset($result['result']['location'])){
- return $result['result']['location'];
- }else{
- WLog('address_to_location',json_encode($result));
- return false;
- }
- }
- // 经纬度转地址
- public function location_to_address($location,$coordtype='wgs84ll')
- {
- $url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=".$this->AK."&output=json&coordtype=$coordtype&location=".$location['lat'].",".$location['lng'];
- $result = json_decode(file_get_contents($url),true);
- if($result['status']==0){
- return $result['result'];
- }else{
- return false;
- }
- }
- // 地点检索
- public function getAddrList($query)
- {
- $query = str_replace(' ','',$query);
- if(!empty($query)){
- $url = "https://api.map.baidu.com/place/v2/search?query=$query&output=json®ion=宜昌市&page_size=10&ak=".$this->AK;
- $result = json_decode(file_get_contents($url),true);
- if($result['status']==0){
- $addressList = array_column($result['results'],'name');
- if(!in_array($query,$addressList)){
- $local = $this->address_to_location($query);
- if($local){
- $result['results'][] = [
- 'name' => $query,
- 'location' => $local,
- 'address' => $query
- ];
- }
- }
- return $result['results'];
- }else{
- WLog('getaddrlist',json_encode($result));
- return false;
- }
- }else{
- return false;
- }
- }
- }
- ?>
|