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.

170 lines
5.2 KiB

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