Geocoder.php 3.1 KB

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