Geocoder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\model;
  3. class Geocoder
  4. {
  5. //百度
  6. static function getbaiduak()
  7. {
  8. $data = Config::getconfig('lbsapi');
  9. if (empty($data['baiduak'])) {
  10. $data = Config::getsitesetupconfig('lbsapi');
  11. }
  12. return $data['baiduak'];
  13. }
  14. //腾讯
  15. static function getqqak()
  16. {
  17. $data = Config::getconfig('lbsapi');
  18. if (empty($data['qqak'])) {
  19. $data = Config::getsitesetupconfig('lbsapi');
  20. }
  21. return $data['qqak'];
  22. }
  23. static function geocoding($address)
  24. {
  25. $address = str_replace("#", "", $address);
  26. $url = 'https://api.map.baidu.com/geocoding/v3/?address=' . $address . '&output=json&ak=' . self::getbaiduak() . '&callback=showLocation';
  27. $ret = urlget($url);
  28. $ret = str_replace('showLocation&&showLocation(', '', $ret);
  29. $ret = str_replace(')', '', $ret);
  30. $ret = json_decode($ret, true);
  31. if (!empty($ret) && $ret['status'] == 0) {
  32. $result['latitude'] = $ret['result']['location']['lat'];
  33. $result['longitude'] = $ret['result']['location']['lng'];
  34. return $result; //返回经纬度结果
  35. } else {
  36. return null;
  37. }
  38. }
  39. static function reverse_geocoding($latitude, $longitude)
  40. {
  41. $ret = self::baidu_reverse_geocoding($latitude, $longitude);
  42. //$ret = self::baidu_reverse_geocoding('22.91824108097', '110.9952491133');
  43. if (empty($ret)) {
  44. $ret = self::qq_reverse_geocoding($latitude, $longitude);
  45. $ret['addressComponent'] = $ret['address_component'];
  46. }
  47. if (!empty($ret)) {
  48. return $ret; //返回经纬度结果
  49. } else {
  50. return null;
  51. }
  52. }
  53. //百度
  54. static function baidu_reverse_geocoding($latitude, $longitude)
  55. {
  56. $url = 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=' . self::getbaiduak() . '&output=json&coordtype=wgs84ll&location=' . $latitude . ',' . $longitude;
  57. $ret = urlget($url);
  58. $ret = str_replace('showLocation&&showLocation(', '', $ret);
  59. $ret = str_replace(')', '', $ret);
  60. $ret = json_decode($ret, true);
  61. if (!empty($ret) && $ret['status'] == 0) {
  62. return $ret['result']; //返回经纬度结果
  63. } else {
  64. return null;
  65. }
  66. }
  67. //腾讯
  68. static function qq_reverse_geocoding($latitude, $longitude)
  69. {
  70. $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $latitude . ',' . $longitude . '&key=' . self::getqqak() . '&get_poi=1';
  71. $ret = urlget($url);
  72. $ret = str_replace('showLocation&&showLocation(', '', $ret);
  73. $ret = str_replace(')', '', $ret);
  74. $ret = json_decode($ret, true);
  75. if (!empty($ret) && $ret['status'] == 0) {
  76. return $ret['result']; //返回经纬度结果
  77. } else {
  78. return null;
  79. }
  80. }
  81. /**
  82. * 根据起点坐标和终点坐标测距离
  83. * @param [array] $from [起点坐标(经纬度),例如:array(118.012951,36.810024)]
  84. * @param [array] $to [终点坐标(经纬度)]
  85. * @param [bool] $km 是否以公里为单位 false:米 true:公里(千米)
  86. * @param [int] $decimal 精度 保留小数位数
  87. * @return [string] 距离数值
  88. */
  89. static function get_distance($from, $to, $km = true, $decimal = 2)
  90. {
  91. sort($from);
  92. sort($to);
  93. $EARTH_RADIUS = 6370.996; // 地球半径系数
  94. $distance = $EARTH_RADIUS * 2 * asin(sqrt(pow(sin(($from[0] * pi() / 180 - $to[0] * pi() / 180) / 2), 2) + cos($from[0] * pi() / 180) * cos($to[0] * pi() / 180) * pow(sin(($from[1] * pi() / 180 - $to[1] * pi() / 180) / 2), 2))) * 1000;
  95. if ($km) {
  96. $distance = $distance / 1000;
  97. }
  98. return round($distance, $decimal);
  99. }
  100. }