Map.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace baidu;
  3. /*
  4. 百度地图接口插件
  5. */
  6. class Map
  7. {
  8. private $AK="";
  9. public function __construct()
  10. {
  11. //执行父类的构造方法
  12. $this->AK = sysconfig('baidu.map_ak');
  13. }
  14. //wgs84转百度坐标
  15. public function wgs84_to_baidu($location)
  16. {
  17. $url='https://api.map.baidu.com/geoconv/v1/?coords='.$location['lng'].','.$location['lat'].'&from=1&to=5&ak='.$this->AK;
  18. $result=json_decode(file_get_contents($url),true);
  19. $result = $result['result'][0];
  20. if(isset($result['x'])){
  21. $loc['lng']=$result['x'];
  22. $loc['lat']=$result['y'];
  23. return $loc;
  24. }else{
  25. return false;
  26. }
  27. }
  28. //地址转经纬度
  29. public function address_to_location($Address)
  30. {
  31. $Address=str_replace(' ','',$Address);
  32. $url='https://api.map.baidu.com/geocoding/v3/?address='.$Address.'&city=宜昌市&output=json&ak='.$this->AK;
  33. $result=json_decode(file_get_contents($url),true);
  34. if(isset($result['result']['location'])){
  35. return $result['result']['location'];
  36. }else{
  37. WLog('address_to_location',json_encode($result));
  38. return false;
  39. }
  40. }
  41. // 经纬度转地址
  42. public function location_to_address($location,$coordtype='wgs84ll')
  43. {
  44. $url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=".$this->AK."&output=json&coordtype=$coordtype&location=".$location['lat'].",".$location['lng'];
  45. $result = json_decode(file_get_contents($url),true);
  46. if($result['status']==0){
  47. return $result['result'];
  48. }else{
  49. return false;
  50. }
  51. }
  52. // 地点检索
  53. public function getAddrList($query)
  54. {
  55. $query = str_replace(' ','',$query);
  56. if(!empty($query)){
  57. $url = "https://api.map.baidu.com/place/v2/search?query=$query&output=json&region=宜昌市&page_size=10&ak=".$this->AK;
  58. $result = json_decode(file_get_contents($url),true);
  59. if($result['status']==0){
  60. $addressList = array_column($result['results'],'name');
  61. if(!in_array($query,$addressList)){
  62. $local = $this->address_to_location($query);
  63. if($local){
  64. $result['results'][] = [
  65. 'name' => $query,
  66. 'location' => $local,
  67. 'address' => $query
  68. ];
  69. }
  70. }
  71. return $result['results'];
  72. }else{
  73. WLog('getaddrlist',json_encode($result));
  74. return false;
  75. }
  76. }else{
  77. return false;
  78. }
  79. }
  80. }
  81. ?>