BDService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\api\service;
  3. class BDService extends BaseService
  4. {
  5. /**
  6. * 道路距离计算
  7. * @param $origin
  8. * @param $destination
  9. * @return \SResult
  10. */
  11. public static function shortestPathsAlgorithm($origin, $destination)
  12. {
  13. $response = http_request("https://api.map.baidu.com/directionlite/v1/driving",
  14. [
  15. "ak" => config("site.baidu_ak"),
  16. "origin" => $origin,
  17. "destination" => $destination,
  18. "steps_info" => 0
  19. ]
  20. );
  21. return new \SResult((int)(0 == $response->status), $response->message, 0 == $response->status ? $response->result->routes[0]->distance : null);
  22. }
  23. public static function geocoding($address)
  24. {
  25. $response = http_request("https://api.map.baidu.com/geocoding/v3/",
  26. [
  27. "ak" => config("site.baidu_ak"),
  28. "output" => 'json',
  29. "address" => $address
  30. ]
  31. );
  32. $code = (int)(0 == $response->status);
  33. return new \SResult(
  34. $code,
  35. $code == 0 ? $response->msg : null,
  36. 0 == $response->status ? ["lng" => $response->result->location->lng, "lat" => $response->result->location->lat] : null
  37. );
  38. }
  39. }