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.

80 lines
2.0 KiB

5 years ago
  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. public function searchMarket($keywords,$cityId = 2163)
  32. {
  33. $res = Market::query()->where([
  34. ['name','like','%'.$keywords.'%'],
  35. ['city_id','=',$cityId]
  36. ])
  37. ->get();
  38. foreach ($res as &$v){
  39. $v->Distance = $this->getDistance($v->lng,$v->lat,108.370333,22.813527);
  40. }
  41. return $res;
  42. }
  43. function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
  44. {
  45. $EARTH_RADIUS = 6370.996; // 地球半径系数
  46. $PI = 3.1415926535898;
  47. $radLat1 = $lat1 * $PI / 180.0;
  48. $radLat2 = $lat2 * $PI / 180.0;
  49. $radLng1 = $lng1 * $PI / 180.0;
  50. $radLng2 = $lng2 * $PI / 180.0;
  51. $a = $radLat1 - $radLat2;
  52. $b = $radLng1 - $radLng2;
  53. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  54. $distance = $distance * $EARTH_RADIUS * 1000;
  55. if ($unit === 2) {
  56. $distance /= 1000;
  57. }
  58. return round($distance, $decimal);
  59. }
  60. //获取服务站信息
  61. function getMarketsInfo()
  62. {
  63. return Market::where('status',1)->get();
  64. }
  65. }