|
32 | 32 |
|
33 | 33 | import com.example.maps3d.common.PositionAndHeading; |
34 | 34 | import com.example.maps3d.common.RouteEngine; |
| 35 | +import com.example.maps3d.common.OahuRouteData; |
35 | 36 | import com.example.maps3dcommon.R; |
36 | 37 | import com.example.maps3djava.BuildConfig; |
37 | 38 | import com.example.maps3djava.sampleactivity.SampleBaseActivity; |
@@ -226,65 +227,63 @@ public void onMap3DViewReady(@NonNull GoogleMap3D googleMap3D) { |
226 | 227 |
|
227 | 228 | private void loadAndRenderRouteAsync(GoogleMap3D googleMap3D) { |
228 | 229 | String apiKey = BuildConfig.MAPS3D_API_KEY; |
229 | | - if (apiKey.isEmpty() || apiKey.contains("YOUR_API_KEY")) { |
230 | | - Toast.makeText(this, "API Key is missing or invalid. Cannot fetch route.", Toast.LENGTH_LONG).show(); |
231 | | - return; |
232 | | - } |
233 | | - |
234 | 230 | LatLng origin = new LatLng(21.307043, -157.858984); |
235 | 231 | LatLng destination = new LatLng(21.390177, -157.719454); |
236 | 232 |
|
237 | | - routeFetchFuture = executorService.submit(routeRepository.fetchRouteCallable(apiKey, origin, destination)); |
238 | 233 | executorService.execute(() -> { |
| 234 | + List<LatLng> decoded; |
239 | 235 | try { |
| 236 | + if (apiKey.isEmpty() || apiKey.contains("YOUR_API_KEY")) { |
| 237 | + throw new Exception("Invalid or missing API Key"); |
| 238 | + } |
| 239 | + routeFetchFuture = executorService.submit(routeRepository.fetchRouteCallable(apiKey, origin, destination)); |
240 | 240 | RouteData routeData = routeFetchFuture.get(); |
241 | | - |
242 | | - // Decode route coords in worker thread (CPU-heavy) |
243 | | - List<LatLng> decoded = PolyUtil.decode(routeData.getEncodedPolyline()); |
244 | | - |
245 | | - // Update UI elements back on standard Main loop thread |
246 | | - mainHandler.post(() -> { |
247 | | - decodedRoute = decoded; |
248 | | - cumulativeDistances = RouteEngine.calculateCumulativeDistances(decoded); |
249 | | - totalDistance = cumulativeDistances[cumulativeDistances.length - 1]; |
250 | | - |
251 | | - // 1. Draw the blue route polyline |
252 | | - List<LatLngAltitude> linePath = new ArrayList<>(); |
253 | | - for (LatLng point : decoded) { |
254 | | - linePath.add(new LatLngAltitude(point.latitude, point.longitude, 0.0)); |
255 | | - } |
256 | | - |
257 | | - PolylineOptions polyOptions = new PolylineOptions(); |
258 | | - polyOptions.setPath(linePath); |
259 | | - polyOptions.setStrokeColor(Color.BLUE); |
260 | | - polyOptions.setStrokeWidth(10.0); |
261 | | - polyOptions.setAltitudeMode(AltitudeMode.CLAMP_TO_GROUND); |
262 | | - polyOptions.setZIndex(5); |
263 | | - routePolyline = googleMap3D.addPolyline(polyOptions); |
264 | | - |
265 | | - // 2. Load the 3D Car model |
266 | | - ModelOptions modelOpts = new ModelOptions(); |
267 | | - modelOpts.setId("vehicle_car_java"); |
268 | | - modelOpts.setPosition(new LatLngAltitude(decoded.get(0).latitude, decoded.get(0).longitude, 25.0)); |
269 | | - modelOpts.setAltitudeMode(AltitudeMode.RELATIVE_TO_GROUND); |
270 | | - modelOpts.setOrientation(new Orientation(0.0, -90.0, 0.0)); |
271 | | - modelOpts.setUrl("https://storage.googleapis.com/gmp-maps-demos/p3d-map/assets/red_car.glb"); |
272 | | - modelOpts.setScale(new Vector3D(50.0, 50.0, 50.0)); |
273 | | - vehicleModel = googleMap3D.addModel(modelOpts); |
274 | | - |
275 | | - updateVehiclePositionAndCamera(); |
276 | | - |
277 | | - // Trigger play automatically once map is populated |
278 | | - togglePlayback(true); |
279 | | - }); |
| 241 | + decoded = PolyUtil.decode(routeData.getEncodedPolyline()); |
280 | 242 | } catch (Exception e) { |
281 | | - Log.e(getTAG(), "Failed to load or decode Honolulu route details", e); |
| 243 | + Log.w(getTAG(), "Routes API fetch failed (" + e.getLocalizedMessage() + "). Falling back to pre-baked Oahu mountain route."); |
| 244 | + decoded = OahuRouteData.getFALLBACK_ROUTE(); |
282 | 245 | mainHandler.post(() -> Toast.makeText( |
283 | 246 | RoutesActivity.this, |
284 | | - "Failed to load route details: " + e.getLocalizedMessage(), |
| 247 | + "Offline: Using local Oahu fallback route", |
285 | 248 | Toast.LENGTH_LONG |
286 | 249 | ).show()); |
287 | 250 | } |
| 251 | + |
| 252 | + final List<LatLng> finalDecoded = decoded; |
| 253 | + mainHandler.post(() -> { |
| 254 | + decodedRoute = finalDecoded; |
| 255 | + cumulativeDistances = RouteEngine.calculateCumulativeDistances(finalDecoded); |
| 256 | + totalDistance = cumulativeDistances[cumulativeDistances.length - 1]; |
| 257 | + |
| 258 | + // 1. Draw the blue route polyline |
| 259 | + List<LatLngAltitude> linePath = new ArrayList<>(); |
| 260 | + for (LatLng point : finalDecoded) { |
| 261 | + linePath.add(new LatLngAltitude(point.latitude, point.longitude, 0.0)); |
| 262 | + } |
| 263 | + |
| 264 | + PolylineOptions polyOptions = new PolylineOptions(); |
| 265 | + polyOptions.setPath(linePath); |
| 266 | + polyOptions.setStrokeColor(Color.BLUE); |
| 267 | + polyOptions.setStrokeWidth(10.0); |
| 268 | + polyOptions.setAltitudeMode(AltitudeMode.CLAMP_TO_GROUND); |
| 269 | + polyOptions.setZIndex(5); |
| 270 | + routePolyline = googleMap3D.addPolyline(polyOptions); |
| 271 | + |
| 272 | + // 2. Load the 3D Car model |
| 273 | + ModelOptions modelOpts = new ModelOptions(); |
| 274 | + modelOpts.setId("vehicle_car_java"); |
| 275 | + modelOpts.setPosition(new LatLngAltitude(finalDecoded.get(0).latitude, finalDecoded.get(0).longitude, 25.0)); |
| 276 | + modelOpts.setAltitudeMode(AltitudeMode.RELATIVE_TO_GROUND); |
| 277 | + modelOpts.setOrientation(new Orientation(0.0, -90.0, 0.0)); |
| 278 | + modelOpts.setUrl("https://storage.googleapis.com/gmp-maps-demos/p3d-map/assets/red_car.glb"); |
| 279 | + modelOpts.setScale(new Vector3D(50.0, 50.0, 50.0)); |
| 280 | + vehicleModel = googleMap3D.addModel(modelOpts); |
| 281 | + |
| 282 | + updateVehiclePositionAndCamera(); |
| 283 | + |
| 284 | + // Trigger play automatically once map is populated |
| 285 | + togglePlayback(true); |
| 286 | + }); |
288 | 287 | }); |
289 | 288 | } |
290 | 289 |
|
|
0 commit comments