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"; } }