-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Overhaul Fly-Along with Nav Waypoints and Supersonic Pacing #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dkhawk
wants to merge
4
commits into
main
Choose a base branch
from
feat/route-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+819
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7a85c45
Add Routes API sample with Map3D integration
dkhawk 243fb8b
Merge remote-tracking branch 'origin/main' into feat/route-sample
dkhawk ce0e688
feat: overhaul fly-along with navigation waypoints and high-velocity …
dkhawk 3467d23
Fix unresolved MAPS_API_KEY reference in RouteSampleActivity
dkhawk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...s/advanced/app/src/main/java/com/example/advancedmaps3dsamples/common/RoutesApiService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.example.advancedmaps3dsamples.common | ||
|
|
||
| import android.util.Log | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.call.body | ||
| import io.ktor.client.engine.cio.CIO | ||
| import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
| import io.ktor.client.request.header | ||
| import io.ktor.client.request.post | ||
| import io.ktor.client.request.setBody | ||
| import io.ktor.client.statement.HttpResponse | ||
| import io.ktor.client.statement.bodyAsText | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.contentType | ||
| import io.ktor.http.isSuccess | ||
| import io.ktor.serialization.kotlinx.json.json | ||
| import kotlinx.serialization.json.Json | ||
|
|
||
| /** | ||
| * Exception thrown when the Routes API returns an error, such as a 403 Forbidden | ||
| * if the API is not enabled for the provided key. | ||
| */ | ||
| class DirectionsErrorException(message: String) : Exception(message) | ||
|
|
||
| /** | ||
| * A simple network service to fetch routes from the Google Maps Routes API. | ||
| * | ||
| * Note: In a production application, making direct API calls to Google Maps Platform | ||
| * services from a client device requires embedding the API key in the app, which | ||
| * poses a security risk. Best practice is to proxy these requests through a secure | ||
| * backend server. This client implementation is provided for demonstration purposes. | ||
| */ | ||
| object RoutesApiService { | ||
|
|
||
| private val client = HttpClient(CIO) { | ||
| install(ContentNegotiation) { | ||
| json(Json { | ||
| ignoreUnknownKeys = true | ||
| encodeDefaults = true | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fetches a route between the origin and destination coordinates. | ||
| * | ||
| * @param apiKey The Google Maps API key (requires Routes API enabled). | ||
| * @param originLat The latitude of the starting point. | ||
| * @param originLng The longitude of the starting point. | ||
| * @param destLat The latitude of the destination point. | ||
| * @param destLng The longitude of the destination point. | ||
| * @return [RoutesResponse] containing the computed route. | ||
| * @throws [DirectionsErrorException] if the API returns a non-success HTTP status. | ||
| */ | ||
| suspend fun fetchRoute( | ||
| apiKey: String, | ||
| originLat: Double, | ||
| originLng: Double, | ||
| destLat: Double, | ||
| destLng: Double | ||
| ): RoutesResponse { | ||
| val requestBody = RoutesRequest( | ||
| origin = Waypoint(Location(RequestLatLng(originLat, originLng))), | ||
| destination = Waypoint(Location(RequestLatLng(destLat, destLng))) | ||
| ) | ||
|
|
||
| val response: HttpResponse = client.post("https://routes.googleapis.com/directions/v2:computeRoutes") { | ||
| contentType(ContentType.Application.Json) | ||
| header("X-Goog-Api-Key", apiKey) | ||
| // Requesting only the most relevant fields to optimize payload size | ||
| header("X-Goog-FieldMask", "routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline,routes.legs.steps.startLocation") | ||
| setBody(requestBody) | ||
| } | ||
|
|
||
| if (response.status.isSuccess()) { | ||
| return response.body() | ||
| } else { | ||
| val errorBody = response.bodyAsText() | ||
| Log.e("RoutesApiService", "Failed to fetch route: ${response.status.value}\n$errorBody") | ||
|
|
||
| // Provide a localized, user-friendly message based on typical API errors | ||
| val userMsg = if (response.status.value == 403) { | ||
| "API Error (HTTP 403). Ensure the Routes API is enabled in the Google Cloud Console for the provided API key." | ||
| } else { | ||
| "Failed to fetch route (HTTP ${response.status.value})." | ||
| } | ||
| throw DirectionsErrorException(userMsg) | ||
| } | ||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
...mples/advanced/app/src/main/java/com/example/advancedmaps3dsamples/common/RoutesModels.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||
| // Copyright 2025 Google LLC | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| // | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| // you may not use this file except in compliance with the License. | ||||||
| // You may obtain a copy of the License at | ||||||
| // | ||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| // | ||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| // See the License for the specific language governing permissions and | ||||||
| // limitations under the License. | ||||||
|
|
||||||
| package com.example.advancedmaps3dsamples.common | ||||||
|
|
||||||
| import kotlinx.serialization.Serializable | ||||||
|
|
||||||
| @Serializable | ||||||
| data class RoutesRequest( | ||||||
| val origin: Waypoint, | ||||||
| val destination: Waypoint, | ||||||
| val travelMode: String = "DRIVE", | ||||||
| val routingPreference: String = "TRAFFIC_AWARE", | ||||||
| val computeAlternativeRoutes: Boolean = false, | ||||||
| val routeModifiers: RouteModifiers = RouteModifiers(), | ||||||
| val languageCode: String = "en-US", | ||||||
| val units: String = "METRIC" | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class Waypoint( | ||||||
| val location: Location | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class Location( | ||||||
| val latLng: RequestLatLng | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class RequestLatLng( | ||||||
| val latitude: Double, | ||||||
| val longitude: Double | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class RouteModifiers( | ||||||
| val avoidTolls: Boolean = false, | ||||||
| val avoidHighways: Boolean = false, | ||||||
| val avoidFerries: Boolean = false | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class RoutesResponse( | ||||||
| val routes: List<Route> = emptyList() | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class Route( | ||||||
| val distanceMeters: Int? = null, | ||||||
| val duration: String? = null, | ||||||
| val polyline: Polyline? = null, | ||||||
| val legs: List<RouteLeg> = emptyList() | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class RouteLeg( | ||||||
| val steps: List<RouteStep> = emptyList() | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class RouteStep( | ||||||
| val startLocation: Location? = null | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| data class Polyline( | ||||||
| val encodedPolyline: String | ||||||
| ) | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have the header check in this repo, but being a new file, it could be worth adding the 2026 year.