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.

111 lines
3.1 KiB

5 years ago
6 years ago
6 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\Service\v3\Interfaces\LocationServiceInterface;
  6. use App\Model\v3\Area;
  7. use App\Model\v3\Market;
  8. class LocationService implements LocationServiceInterface
  9. {
  10. public function do()
  11. {
  12. // TODO: Implement do() method.
  13. }
  14. public function check()
  15. {
  16. // TODO: Implement check() method.
  17. }
  18. public function undo()
  19. {
  20. // TODO: Implement undo() method.
  21. }
  22. public function getMarketListByLocation($lng,$lat)
  23. {
  24. $cityIds = Market::query()->pluck('city_id');
  25. $res = Area::query()->with('markets')->whereIn('id',$cityIds)->get();
  26. foreach ($res as &$v){
  27. foreach ($v->markets as &$m)
  28. {
  29. if(!empty($lng) && !empty($lat)){
  30. $m->Distance = ($this->getDistance($m->lng,$m->lat,$lng,$lat)).' km';
  31. }else{
  32. $m->Distance = '';
  33. }
  34. }
  35. }
  36. return $res;
  37. }
  38. public function searchMarket($keywords,$lng,$lat,$cityId = 2163)
  39. {
  40. $res = Market::query()->where([
  41. ['name','like','%'.$keywords.'%'],
  42. ['city_id','=',$cityId]
  43. ])
  44. ->get();
  45. foreach ($res as &$v){
  46. if(!empty($lng) && !empty($lat)){
  47. $v->Distance = ($this->getDistance($v->lng,$v->lat,$lng,$lat)).' km';
  48. }else{
  49. $v->Distance = '';
  50. }
  51. }
  52. return $res;
  53. }
  54. public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
  55. {
  56. $EARTH_RADIUS = 6370.996; // 地球半径系数
  57. $PI = 3.1415926535898;
  58. $radLat1 = $lat1 * $PI / 180.0;
  59. $radLat2 = $lat2 * $PI / 180.0;
  60. $radLng1 = $lng1 * $PI / 180.0;
  61. $radLng2 = $lng2 * $PI / 180.0;
  62. $a = $radLat1 - $radLat2;
  63. $b = $radLng1 - $radLng2;
  64. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  65. $distance = $distance * $EARTH_RADIUS * 1000;
  66. if ($unit === 2) {
  67. $distance /= 1000;
  68. }
  69. return round($distance, $decimal);
  70. }
  71. //获取服务站信息
  72. function getMarketsInfo()
  73. {
  74. return Market::where('status',1)->get();
  75. }
  76. //获取骑行距离
  77. public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2)
  78. {
  79. $url = 'https://apis.map.qq.com/ws/direction/v1/';
  80. /**
  81. * $tyep driving 驾车 walking 步行 bicycling 骑行 transit 公交
  82. */
  83. $type = 'bicycling';
  84. $key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL';
  85. $result = file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
  86. $resultArr = json_decode($result,true);
  87. if($resultArr['status'] > 0)
  88. {
  89. return 1000;
  90. //throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
  91. }
  92. $distance = $resultArr['result']['routes'][0]['distance'];
  93. return $distance;
  94. }
  95. }