Maps.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\Google;
  4. use _PHPStan_579402b64\Nette\Utils\DateTime;
  5. use App\Master\Framework\Library\Library;
  6. class Maps extends Library
  7. {
  8. public string $base_uri = '';// 请求域名地址
  9. private string $key;
  10. /**
  11. * 实例化
  12. */
  13. public function __construct()
  14. {
  15. // 获取配置信息
  16. $this->key = (string)site('google_map_api_key');
  17. }
  18. /**
  19. * 计算两地距离,行驶时长等
  20. * @param float $start_lng 经度
  21. * @param float $start_lat 纬度
  22. * @param float $end_lng
  23. * @param float $end_lat
  24. * @param int $departureTime
  25. * @param float $point_lng 途径点 经度
  26. * @param float $point_lat 途径点 纬度
  27. * @return bool
  28. */
  29. public function computeRoutes(float $start_lng, float $start_lat, float $end_lng, float $end_lat, int $departureTime = 0, float $point_lng = 0, float $point_lat = 0): bool
  30. {
  31. // $departureTime = $this->gmt_rfc3339($departureTime == 0 ? time() : $departureTime);
  32. $data = [
  33. 'origin' => [
  34. 'location' => [
  35. 'latLng' => [
  36. 'latitude' => $start_lat,
  37. 'longitude' => $start_lng
  38. ]
  39. ]
  40. ],
  41. 'destination' => [
  42. 'location' => [
  43. 'latLng' => [
  44. 'latitude' => $end_lat,
  45. 'longitude' => $end_lng
  46. ]
  47. ]
  48. ],
  49. 'travelMode' => 'DRIVE',// 方式 DRIVE 驾车
  50. "routingPreference" => "TRAFFIC_AWARE",
  51. //"departureTime" => $departureTime,// 出发时间 "2024-07-09T21:01:23Z"
  52. "computeAlternativeRoutes" => false,// 备选路线
  53. "routeModifiers" => [
  54. "avoidTolls" => true,
  55. "avoidHighways" => false,
  56. "avoidFerries" => false
  57. ],
  58. "languageCode" => "en-US",
  59. "units" => "IMPERIAL"
  60. ];
  61. if (!empty($point_lng) && !empty($point_lat)) {
  62. $data['intermediates'] = [
  63. [
  64. 'location' => [
  65. 'latLng' => [
  66. 'latitude' => $point_lat,
  67. 'longitude' => $point_lng
  68. ]
  69. ]
  70. ]
  71. ];
  72. }
  73. $header = [
  74. 'X-Goog-FieldMask' => 'routes.duration,routes.distanceMeters'
  75. ];
  76. $this->base_uri = 'https://routes.googleapis.com';
  77. $response = $this->post('/directions/v2:computeRoutes', $data, $header);
  78. if ($response->getStatusCode() != 200) {
  79. return $this->error($response->getReasonPhrase());
  80. }
  81. $json = $response->getBody()->getContents();
  82. $body = json_decode($json, true);
  83. return $this->success('获取成功', $body);
  84. }
  85. /**
  86. * 地址搜索
  87. * @param string $input
  88. * @return bool
  89. */
  90. public function place_search(string $input)
  91. {
  92. $data = [
  93. 'fields' => 'formatted_address,name,geometry,place_id',
  94. 'inputtype' => 'textquery',
  95. 'input' => $input,
  96. 'sensor' => false,
  97. 'key' => $this->key
  98. ];
  99. $response = $this->get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', $data);
  100. if ($response->getStatusCode() != 200) {
  101. return $this->error($response->getReasonPhrase());
  102. }
  103. $json = $response->getBody()->getContents();
  104. $body = json_decode($json, true);
  105. if (!isset($body['status']) || $body['status'] != 'OK') {
  106. return $this->error($body['error_message'] ?? '获取失败', $body);
  107. }
  108. return $this->success('获取成功', $body);
  109. }
  110. /**
  111. * 地址搜索(自动补全)
  112. * @param string $input
  113. * @param string $latitude
  114. * @param string $longitude
  115. * @param string $sessionToken
  116. * @return bool
  117. */
  118. public function place_auto_search(string $input, string $latitude, string $longitude, string $sessionToken = '')
  119. {
  120. $data = [
  121. 'key' => $this->key,
  122. 'input' => $input,
  123. 'components' => 'country:ca',
  124. 'location' => "{$latitude},{$longitude}",
  125. 'sessiontoken' => $sessionToken,
  126. 'language' => 'zh'
  127. ];
  128. $response = $this->get('https://maps.googleapis.com/maps/api/place/autocomplete/json', $data);
  129. if ($response->getStatusCode() != 200) {
  130. return $this->error($response->getReasonPhrase());
  131. }
  132. $json = $response->getBody()->getContents();
  133. $body = json_decode($json, true);
  134. if (!isset($body['status']) || $body['status'] != 'OK') {
  135. return $this->error($body['error_message'] ?? '获取失败', $body);
  136. }
  137. return $this->success('获取成功', $body);
  138. }
  139. /**
  140. * 地址详情接口
  141. * @param string $place_id
  142. * @return bool
  143. */
  144. public function place_details(string $place_id)
  145. {
  146. $data = [
  147. 'fields' => 'address_components,geometry',
  148. 'place_id' => $place_id,
  149. 'key' => $this->key
  150. ];
  151. $response = $this->get('https://maps.googleapis.com/maps/api/place/details/json', $data);
  152. if ($response->getStatusCode() != 200) {
  153. return $this->error($response->getReasonPhrase());
  154. }
  155. $json = $response->getBody()->getContents();
  156. $body = json_decode($json, true);
  157. if (!isset($body['status']) || $body['status'] != 'OK') {
  158. return $this->error($body['error_message'] ?? '获取失败', $body);
  159. }
  160. return $this->success('获取成功', $body);
  161. }
  162. /**
  163. * 地址搜索
  164. * @param string $latlng
  165. * @return bool
  166. */
  167. public function geocode(string $latlng)
  168. {
  169. $data = [
  170. 'latlng' => $latlng,
  171. 'sensor' => false,
  172. 'key' => $this->key
  173. ];
  174. $response = $this->get('https://maps.google.com/maps/api/geocode/json', $data);
  175. if ($response->getStatusCode() != 200) {
  176. return $this->error($response->getReasonPhrase());
  177. }
  178. $json = $response->getBody()->getContents();
  179. $body = json_decode($json, true);
  180. if (!isset($body['status']) || $body['status'] != 'OK') {
  181. return $this->error($body['error_message'] ?? '获取失败', $body);
  182. }
  183. return $this->success('获取成功', $body);
  184. }
  185. /**
  186. * 路线规划
  187. * @param string $destination
  188. * @param string $origin
  189. * @param string $waypoints
  190. * @return bool
  191. */
  192. public function directions(string $destination, string $origin, string $waypoints = '')
  193. {
  194. $data = [
  195. 'destination' => $destination,
  196. 'origin' => $origin,
  197. 'waypoints' => $waypoints,
  198. 'avoid' => 'tolls',
  199. 'key' => $this->key
  200. ];
  201. $response = $this->get('https://maps.googleapis.com/maps/api/directions/json', $data);
  202. if ($response->getStatusCode() != 200) {
  203. return $this->error($response->getReasonPhrase());
  204. }
  205. $json = $response->getBody()->getContents();
  206. $body = json_decode($json, true);
  207. if (!isset($body['status']) || $body['status'] != 'OK') {
  208. return $this->error($body['error_message'] ?? '获取失败', $body);
  209. }
  210. return $this->success('获取成功', $body);
  211. }
  212. private function post(string $uri, array $params = [], array $header = [])
  213. {
  214. $header['X-Goog-Api-Key'] = $this->key;
  215. return $this->postJson($uri, $params, $header);
  216. }
  217. private function get(string $uri, array $params = [], array $header = [])
  218. {
  219. return $this->getJson($uri, $params, $header);
  220. }
  221. private function gmt_iso8601($time)
  222. {
  223. $dtStr = date("c", $time);
  224. $dateTime = new \DateTime($dtStr);
  225. $expiration = $dateTime->format(\DateTime::ISO8601);
  226. $pos = strpos($expiration, '+');
  227. $expiration = substr($expiration, 0, $pos);
  228. return $expiration . "Z";
  229. }
  230. private function gmt_rfc3339($time)
  231. {
  232. $dateTime = new \DateTime();
  233. $dateTime->setTimestamp($time);
  234. $expiration = $dateTime->format(\DateTime::ATOM);
  235. $pos = strrpos($expiration, '-');
  236. $expiration = substr($expiration, 0, $pos);
  237. return $expiration . "Z";
  238. }
  239. }