123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- declare(strict_types=1);
- namespace App\Master\Framework\Library\Google;
- use _PHPStan_579402b64\Nette\Utils\DateTime;
- use App\Master\Framework\Library\Library;
- class Maps extends Library
- {
- public string $base_uri = '';// 请求域名地址
- private string $key;
- /**
- * 实例化
- */
- public function __construct()
- {
- // 获取配置信息
- $this->key = (string)site('google_map_api_key');
- }
- /**
- * 计算两地距离,行驶时长等
- * @param float $start_lng 经度
- * @param float $start_lat 纬度
- * @param float $end_lng
- * @param float $end_lat
- * @param int $departureTime
- * @param float $point_lng 途径点 经度
- * @param float $point_lat 途径点 纬度
- * @return bool
- */
- 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
- {
- // $departureTime = $this->gmt_rfc3339($departureTime == 0 ? time() : $departureTime);
- $data = [
- 'origin' => [
- 'location' => [
- 'latLng' => [
- 'latitude' => $start_lat,
- 'longitude' => $start_lng
- ]
- ]
- ],
- 'destination' => [
- 'location' => [
- 'latLng' => [
- 'latitude' => $end_lat,
- 'longitude' => $end_lng
- ]
- ]
- ],
- 'travelMode' => 'DRIVE',// 方式 DRIVE 驾车
- "routingPreference" => "TRAFFIC_AWARE",
- //"departureTime" => $departureTime,// 出发时间 "2024-07-09T21:01:23Z"
- "computeAlternativeRoutes" => false,// 备选路线
- "routeModifiers" => [
- "avoidTolls" => true,
- "avoidHighways" => false,
- "avoidFerries" => false
- ],
- "languageCode" => "en-US",
- "units" => "IMPERIAL"
- ];
- if (!empty($point_lng) && !empty($point_lat)) {
- $data['intermediates'] = [
- [
- 'location' => [
- 'latLng' => [
- 'latitude' => $point_lat,
- 'longitude' => $point_lng
- ]
- ]
- ]
- ];
- }
- $header = [
- 'X-Goog-FieldMask' => 'routes.duration,routes.distanceMeters'
- ];
- $this->base_uri = 'https://routes.googleapis.com';
- $response = $this->post('/directions/v2:computeRoutes', $data, $header);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- return $this->success('获取成功', $body);
- }
- /**
- * 地址搜索
- * @param string $input
- * @return bool
- */
- public function place_search(string $input)
- {
- $data = [
- 'fields' => 'formatted_address,name,geometry,place_id',
- 'inputtype' => 'textquery',
- 'input' => $input,
- 'sensor' => false,
- 'key' => $this->key
- ];
- $response = $this->get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (!isset($body['status']) || $body['status'] != 'OK') {
- return $this->error($body['error_message'] ?? '获取失败', $body);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 地址搜索(自动补全)
- * @param string $input
- * @param string $latitude
- * @param string $longitude
- * @param string $sessionToken
- * @return bool
- */
- public function place_auto_search(string $input, string $latitude, string $longitude, string $sessionToken = '')
- {
- $data = [
- 'key' => $this->key,
- 'input' => $input,
- 'components' => 'country:ca',
- 'location' => "{$latitude},{$longitude}",
- 'sessiontoken' => $sessionToken,
- 'language' => 'zh'
- ];
- $response = $this->get('https://maps.googleapis.com/maps/api/place/autocomplete/json', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (!isset($body['status']) || $body['status'] != 'OK') {
- return $this->error($body['error_message'] ?? '获取失败', $body);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 地址详情接口
- * @param string $place_id
- * @return bool
- */
- public function place_details(string $place_id)
- {
- $data = [
- 'fields' => 'address_components,geometry',
- 'place_id' => $place_id,
- 'key' => $this->key
- ];
- $response = $this->get('https://maps.googleapis.com/maps/api/place/details/json', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (!isset($body['status']) || $body['status'] != 'OK') {
- return $this->error($body['error_message'] ?? '获取失败', $body);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 地址搜索
- * @param string $latlng
- * @return bool
- */
- public function geocode(string $latlng)
- {
- $data = [
- 'latlng' => $latlng,
- 'sensor' => false,
- 'key' => $this->key
- ];
- $response = $this->get('https://maps.google.com/maps/api/geocode/json', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (!isset($body['status']) || $body['status'] != 'OK') {
- return $this->error($body['error_message'] ?? '获取失败', $body);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 路线规划
- * @param string $destination
- * @param string $origin
- * @param string $waypoints
- * @return bool
- */
- public function directions(string $destination, string $origin, string $waypoints = '')
- {
- $data = [
- 'destination' => $destination,
- 'origin' => $origin,
- 'waypoints' => $waypoints,
- 'avoid' => 'tolls',
- 'key' => $this->key
- ];
- $response = $this->get('https://maps.googleapis.com/maps/api/directions/json', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (!isset($body['status']) || $body['status'] != 'OK') {
- return $this->error($body['error_message'] ?? '获取失败', $body);
- }
- return $this->success('获取成功', $body);
- }
- private function post(string $uri, array $params = [], array $header = [])
- {
- $header['X-Goog-Api-Key'] = $this->key;
- return $this->postJson($uri, $params, $header);
- }
- private function get(string $uri, array $params = [], array $header = [])
- {
- return $this->getJson($uri, $params, $header);
- }
- private function gmt_iso8601($time)
- {
- $dtStr = date("c", $time);
- $dateTime = new \DateTime($dtStr);
- $expiration = $dateTime->format(\DateTime::ISO8601);
- $pos = strpos($expiration, '+');
- $expiration = substr($expiration, 0, $pos);
- return $expiration . "Z";
- }
- private function gmt_rfc3339($time)
- {
- $dateTime = new \DateTime();
- $dateTime->setTimestamp($time);
- $expiration = $dateTime->format(\DateTime::ATOM);
- $pos = strrpos($expiration, '-');
- $expiration = substr($expiration, 0, $pos);
- return $expiration . "Z";
- }
- }
|