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.

101 lines
2.8 KiB

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($cityIds)
  23. {
  24. $res = Area::query()->with('markets')->whereIn('id',$cityIds)->get();
  25. foreach ($res as &$v){
  26. foreach ($v->markets as &$m)
  27. {
  28. $m->Distance = $this->getDistance($m->lng,$m->lat,108.370333,22.813527);
  29. }
  30. }
  31. return $res;
  32. }
  33. public function searchMarket($keywords,$cityId = 2163)
  34. {
  35. $res = Market::query()->where([
  36. ['name','like','%'.$keywords.'%'],
  37. ['city_id','=',$cityId]
  38. ])
  39. ->get();
  40. foreach ($res as &$v){
  41. $v->Distance = $this->getDistance($v->lng,$v->lat,108.370333,22.813527);
  42. }
  43. return $res;
  44. }
  45. public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
  46. {
  47. $EARTH_RADIUS = 6370.996; // 地球半径系数
  48. $PI = 3.1415926535898;
  49. $radLat1 = $lat1 * $PI / 180.0;
  50. $radLat2 = $lat2 * $PI / 180.0;
  51. $radLng1 = $lng1 * $PI / 180.0;
  52. $radLng2 = $lng2 * $PI / 180.0;
  53. $a = $radLat1 - $radLat2;
  54. $b = $radLng1 - $radLng2;
  55. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  56. $distance = $distance * $EARTH_RADIUS * 1000;
  57. if ($unit === 2) {
  58. $distance /= 1000;
  59. }
  60. return round($distance, $decimal);
  61. }
  62. //获取服务站信息
  63. function getMarketsInfo()
  64. {
  65. return Market::where('status',1)->get();
  66. }
  67. //获取骑行距离
  68. public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2)
  69. {
  70. $url = 'https://apis.map.qq.com/ws/direction/v1/';
  71. /**
  72. * $tyep driving 驾车 walking 步行 bicycling 骑行 transit 公交
  73. */
  74. $type = 'bicycling';
  75. $key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL';
  76. $result = file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
  77. $resultArr = json_decode($result,true);
  78. if($resultArr['status'] > 0)
  79. {
  80. throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
  81. }
  82. $distance = $resultArr['result']['routes'][0]['distance'];
  83. return $distance;
  84. }
  85. }