You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Model\v3\UserCollection;
  6. use App\Service\v3\Interfaces\LocationServiceInterface;
  7. use App\Model\v3\Area;
  8. use App\Model\v3\Market;
  9. class LocationService implements LocationServiceInterface
  10. {
  11. public function do()
  12. {
  13. // TODO: Implement do() method.
  14. }
  15. public function check()
  16. {
  17. // TODO: Implement check() method.
  18. }
  19. public function undo()
  20. {
  21. // TODO: Implement undo() method.
  22. }
  23. public function getMarketListByLocation($lng,$lat)
  24. {
  25. $cityIds = Market::query()->pluck('city_id');
  26. $res = Area::query()->with('markets')->whereIn('id',$cityIds)->get();
  27. foreach ($res as &$v){
  28. if(!empty($lng) && !empty($lat)) {
  29. $v->distance_num = $this->getDistance($v->lng, $v->lat, $lng, $lat);
  30. $v->distance = $v->distance_num . ' km';
  31. } else {
  32. $v->distance_num = 0;
  33. $v->distance = '';
  34. }
  35. foreach ($v->markets as &$m)
  36. {
  37. if(!empty($lng) && !empty($lat)){
  38. $m->distance_num = $this->getDistance($m->lng, $m->lat, $lng, $lat);
  39. $m->distance = $m->distance_num.' km';
  40. }else{
  41. $m->distance_num = 0;
  42. $m->distance = '';
  43. }
  44. }
  45. }
  46. $res = collect($res->toArray())->sortBy(function ($area, $key) {
  47. return $area['distance_num'];
  48. });
  49. $res = collect($res->all())->map(function ($area, $key) {
  50. $markets = collect($area['markets'])->sortBy('distance_num');
  51. $area['markets'] = $markets->values()->all();
  52. return $area;
  53. });
  54. return $res->all();
  55. }
  56. public function searchMarket($keywords,$lng,$lat,$cityId = 2163)
  57. {
  58. $res = Market::query()->where([
  59. ['name','like','%'.$keywords.'%'],
  60. ['city_id','=',$cityId]
  61. ])
  62. ->get();
  63. foreach ($res as &$v){
  64. if(!empty($lng) && !empty($lat)){
  65. $v->distance = ($this->getDistance($v->lng,$v->lat,$lng,$lat)).' km';
  66. }else{
  67. $v->distance = '';
  68. }
  69. }
  70. return $res;
  71. }
  72. public function getNearestMarket($lng,$lat)
  73. {
  74. $markets = Market::query()->get()->toArray();
  75. $result = [];
  76. $distinctMin = 0;
  77. foreach ($markets as &$market){
  78. $distinct = $this->getDistance($lng,$lat,$market['lng'],$market['lat'],1);
  79. $market['distinct'] = round(($distinct/1000),2).' km';
  80. if($distinct < $distinctMin || $distinctMin === 0) {
  81. $distinctMin = $distinct;
  82. $result = $market;
  83. }
  84. }
  85. return $result;
  86. }
  87. /**
  88. * @param $lng1
  89. * @param $lat1
  90. * @param $lng2
  91. * @param $lat2
  92. * @param int $unit 单位 1: 2:公里
  93. * @param int $decimal 精度 保留小数位数
  94. * @return false|float
  95. */
  96. public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
  97. {
  98. $EARTH_RADIUS = 6370.996; // 地球半径系数
  99. $PI = 3.1415926535898;
  100. $radLat1 = $lat1 * $PI / 180.0;
  101. $radLat2 = $lat2 * $PI / 180.0;
  102. $radLng1 = $lng1 * $PI / 180.0;
  103. $radLng2 = $lng2 * $PI / 180.0;
  104. $a = $radLat1 - $radLat2;
  105. $b = $radLng1 - $radLng2;
  106. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  107. $distance = $distance * $EARTH_RADIUS * 1000;
  108. if ($unit === 2) {
  109. $distance /= 1000;
  110. }
  111. return round($distance, $decimal);
  112. }
  113. //获取服务站信息
  114. function getMarketsInfo()
  115. {
  116. return Market::where('status',1)->get();
  117. }
  118. //获取骑行距离
  119. public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2)
  120. {
  121. $url = 'https://apis.map.qq.com/ws/direction/v1/';
  122. /**
  123. * $tyep driving 驾车 walking 步行 bicycling 骑行 transit 公交
  124. */
  125. $type = 'bicycling';
  126. $key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL';
  127. $result = file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
  128. $resultArr = json_decode($result,true);
  129. if($resultArr['status'] > 0)
  130. {
  131. return 1000;
  132. //throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
  133. }
  134. $distance = $resultArr['result']['routes'][0]['distance'];
  135. return $distance;
  136. }
  137. }