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.

61 lines
1.5 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Service\v3\Interfaces\LocationServiceInterface;
  4. use App\Model\v3\Area;
  5. use App\Model\v3\Market;
  6. class LocationService implements LocationServiceInterface
  7. {
  8. public function do()
  9. {
  10. // TODO: Implement do() method.
  11. }
  12. public function check()
  13. {
  14. // TODO: Implement check() method.
  15. }
  16. public function undo()
  17. {
  18. // TODO: Implement undo() method.
  19. }
  20. public function getMarketListByLocation($cityIds)
  21. {
  22. $res = Area::query()->with('markets')->whereIn('id',$cityIds)->get();
  23. foreach ($res as &$v){
  24. foreach ($v->markets as &$m)
  25. {
  26. $m->Distance = $this->getDistance($m->lng,$m->lat,108.370333,22.813527);
  27. }
  28. }
  29. return $res;
  30. }
  31. function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
  32. {
  33. $EARTH_RADIUS = 6370.996; // 地球半径系数
  34. $PI = 3.1415926535898;
  35. $radLat1 = $lat1 * $PI / 180.0;
  36. $radLat2 = $lat2 * $PI / 180.0;
  37. $radLng1 = $lng1 * $PI / 180.0;
  38. $radLng2 = $lng2 * $PI / 180.0;
  39. $a = $radLat1 - $radLat2;
  40. $b = $radLng1 - $radLng2;
  41. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  42. $distance = $distance * $EARTH_RADIUS * 1000;
  43. if ($unit === 2) {
  44. $distance /= 1000;
  45. }
  46. return round($distance, $decimal);
  47. }
  48. }