import 'dart:async'; import 'dart:convert'; import…
- User
import 'dart:async'; import 'dart:convert'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:flutter_polyline_points/flutter_polyline_points.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:monk_food/controller/data_importer.dart';
class MapProvider with ChangeNotifier { List markers = []; PolylinePoints polylinePoints = PolylinePoints(); Map<PolylineId, Polyline> polylines = {}; late GoogleMapController googleMapController;
// void initialize(List locs) { // markers = []; // for (var loc in locs) { // addMarker(loc); // } // cacheMarkers(); // }
void initialize(List locs) { markers.clear(); int a = 1;
for (var loc in locs) { addMarker(loc); print("loc $a: ${loc.longitude} ${loc.latitude}"); a += 1; } if (markers.length > 1) { getDirections(); } cacheMarkers();}
void setMapController(GoogleMapController controller) { googleMapController = controller; notifyListeners(); }
void addMarker(LatLng latLng) { markers.add( Marker( consumeTapEvents: true, markerId: MarkerId(latLng.toString()), position: latLng, onTap: () { markers.removeWhere((element) => element.markerId == MarkerId(latLng.toString())); if (markers.length > 1) { getDirections(); } else { polylines.clear(); } cacheMarkers(); cachePolylines(); notifyListeners(); }, ), ); if (markers.length > 1) { getDirections(); } cacheMarkers(); notifyListeners(); }
Future getDirections() async { final connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult.contains(ConnectivityResult.none)) { print("No internet connection. Loading cached data..."); await loadCachedPolylines(); notifyListeners(); return; } List<LatLng> polylineCoordinates = []; List<PolylineWayPoint> polylineWayPoints = []; for (var marker in markers) { polylineWayPoints.add(PolylineWayPoint( location: "${marker.position.latitude},${marker.position.longitude}", stopOver: true, )); } try { PolylineResult result = await polylinePoints.getRouteBetweenCoordinates( request: PolylineRequest( origin: PointLatLng(markers.first.position.latitude, markers.first.position.longitude), destination: PointLatLng(markers.last.position.latitude, markers.last.position.longitude), mode: TravelMode.driving, wayPoints: polylineWayPoints, ), googleApiKey: googleMapApiKey, ); if (result.points.isNotEmpty) { for (var point in result.points) { polylineCoordinates.add(LatLng(point.latitude, point.longitude)); } } else { print(result.errorMessage); } addPolyLine(polylineCoordinates); cachePolylines(); } catch (e) { print("Failed to fetch directions: $e"); // Fallback to cached data if available await loadCachedPolylines(); } notifyListeners();}
void addPolyLine(List polylineCoordinates) { PolylineId id = PolylineId("poly"); Polyline polyline = Polyline( polylineId: id, color: Colors.red, points: polylineCoordinates, width: 4, ); polylines[id] = polyline; notifyListeners(); }
- Assistant
- User
whe,it,fall,} catch (e) { print("Failed to fetch directions: $e"); await loadCachedPolylines(); },because,api,return,ZERO_RESULTS,i,want,to,still,showmap
- Assistant
- User
try { PolylineResult result = await polylinePoints.getRouteBetweenCoordinates( request: PolylineRequest( origin: PointLatLng(markers.first.position.latitude, markers.first.position.longitude), destination: PointLatLng(markers.last.position.latitude, markers.last.position.longitude), mode: TravelMode.driving, wayPoints: polylineWayPoints, ), googleApiKey: googleMapApiKey, );
try { for (var point in result.points) { polylineCoordinates.add(LatLng(point.latitude, point.longitude)); } } catch (e) { print('Failed to fetch directions ${result.errorMessage}'); } addPolyLine(polylineCoordinates); cachePolylines(); } catch (e) { print('Failed to fetch directions catch'); print("Failed to fetch directions: $e"); // Fallback to cached data if available await loadCachedPolylines(); }I/flutter ( 4901): Failed to fetch directions catchI/flutter ( 4901): Failed to fetch directions: Exception: Unable to get route: Response ---> ZERO_RESULTS I/flutter ( 4901): Failed to fetch directions catch I/flutter ( 4901): Failed to fetch directions: Exception: Unable to get route: Response ---> ZERO_RESULTS I/flutter ( 4901): Failed to fetch directions catch I/flutter ( 4901): Failed to fetch directions: Exception: Unable to get route: Response ---> ZERO_RESULTS <now,the,map,dissaperaed,i,dont,want,that,handle,it
- Assistant
- User
- Assistant
- User
GoogleMap created successfully but,not,showing
- Assistant