createRoute method

Future<void> createRoute(
  1. String currentLatLng,
  2. List<String> destinationLatLng,
  3. String color,
  4. int width,
  5. String apiKey,
)

Implementation

Future<void> createRoute(String currentLatLng, List<String> destinationLatLng, String color, int width, String apiKey) async {
  List<Map<String, dynamic>> tasks = [];
  for (int i = 0; i < destinationLatLng.length; i++) {
    var lat = destinationLatLng[i].split(";")[0];
    var lng = destinationLatLng[i].split(";")[1];

    tasks.add({"lat": lat, "lng": lng, "id": i + 1});
  }

  var riderCurrentLocation = {
    "lat": currentLatLng.split(";")[0],
    "lng": currentLatLng.split(";")[1],
    "id": 1
  };

  final url = Uri.parse('https://api1.tplmaps.com:8888/route/optimal?isRouteGeometry=true&isRoundTrip=false&elementCount=${tasks.length}&apikey=$apiKey');

  // print("check kro bhai");
  // print(url);
  // print(riderCurrentLocation);
  //
  // print("check kro bhai  2 ");
  // print(tasks);

  try {
    final response = await http.post(
      url,
      headers: {
        'Content-Type': 'application/json',
      },
      body: jsonEncode({
        "rider_current_location": riderCurrentLocation,
        "tasks": tasks,
      }),
    );



    if (response.statusCode == 200) {
      final responseData = jsonDecode(response.body);
      final route = responseData['route'];
      final latLngArray = route.map<String>((coord) => '${coord[0]};${coord[1]}').toList();

      // print("check kro bhai 3 ");
      // print(latLngArray);
      //
      // print("check kro bhai 4 ");
      // print(responseData);

      _channel.invokeMethod('addPolyLines' , {'polylineData': latLngArray , 'color': color , 'width': width});


    } else {
      throw Exception('Failed to load data');
    }
  } catch (error) {
    print(error);
  }
}