BIS Tech
BIS Tech

Reputation: 19504

how to get real direction steps using Flutter? (draw real route)

I'm working with taxi app. Is it possible to draw real route on real road using flutter?

I used this way: https://developers.google.com/maps/documentation/directions/intro#ExampleRequests

  getDirectionSteps(double destinationLat, double destinationLng) {
    network
        .get("origin=" +
            location.latitude.toString() +
            "," +
            location.longitude.toString() +
            "&destination=" +
            destinationLat.toString() +
            "," +
            destinationLng.toString() +
            "&key=api_key")
        .then((dynamic res) {
      List<Steps> rr = res;
      print(res.toString());

      ccc = new List();
      for (final i in rr) {
        ccc.add(i.startLocation);
        ccc.add(i.endLocation);
      }

      mapView.onMapReady.listen((_) {
        mapView.setMarkers(getMarker(location.latitude,location.longitude,destinationLat,destinationLng));
        mapView.addPolyline(new Polyline("12", ccc, width: 15.0));
      });
      _screenListener.dismissLoader();
      showMap();
    }).catchError((Exception error) => _screenListener.dismissLoader());
  }

my output look like this:

enter image description here

But I need like this: (draw destination route on real road)

enter image description here

Upvotes: 1

Views: 6656

Answers (1)

Sahdeep Singh
Sahdeep Singh

Reputation: 1442

to find the exact route you have to get points out of the polylineoverview from the json from directions api.

This is how I extracted the exact route just like you show in the second image.

Its is a function that return points as a string

Future<String> getRouteCoordinates(LatLng l1, LatLng l2) async {
    String url =
        "https://maps.googleapis.com/maps/api/directions/json?origin=${l1.latitude},${l1.longitude}&destination=${l2.latitude},${l2.longitude}&key=${Constants.anotherApiKey}";
    http.Response response = await http.get(url);
    Map values = jsonDecode(response.body);
    ProjectLog.logIt(TAG, "Predictions", values.toString());
    return values["routes"][0]["overview_polyline"]["points"];
  }

you will get a string of points somewhat similiar to this

 u|umDs`gwML}A_GGgFGAWwDEm@TcFGsAAa@BeA\QDe@AYISOKHKJGFw@^?jAAnAEnOA|GAhHEx@?jA@tC?XFLLf@Bf@@t@?xAA|E?dEEj_@GxMChG@tCIvl@@tAK`DQlA?zBApE?lBExNAlH@rMAtGJdDJnATfB`AnEdAzEj@~B|@lEF\xAvGnAlF~@lEv@`DvAxFxAxGzCdN`H`ZnEnRr@hDnB|IhDlNvKnd@vDhPrFzUzGjYxBtH|@hCdAzBXl@fAhBtAtBjBhCfArAdAhAvBtBlB|AjGfFhLzJfEzDzCvDz@pA`BpC`ApBbAzBxCrIr@rBjNta@x@nBbAlBzCbI|R|j@hA`FBVC`ASpD?lA[FiMpCaBVgABiAPoE~@cIdBiLfCcHdBsCl@yJvBmDt@y@l@{@X_@P[VGJGZCd@r@tCf@rBTbAV`BB`@?n@GdA@XHj@bAxBl@hBPjADf@?v@Ej@Ml@Ut@[r@]h@sA`C{@lAMZGl@KjECbDGhBuGMsJKcCGw@CqJCiECAd@ALoBbKs@jDM^x@j@vPfLvCnB~DnCx@f@R@RAd@GDIbBmDv@y@LId@On@A~EJX@pDJrADb@QFC

Now you will add the polyline in the set of polylines like this

_polyLines.add(Polyline(
        polylineId: PolylineId(Constants.currentRoutePolylineId),//pass any string here
        width: 3,
        geodesic: true,
        points: Utils.convertToLatLng(Utils.decodePoly(encodedPoly)),
        color: ConstantColors.PrimaryColor));

here encodedPoly is the same string that extracted from previous method.

In the function above you have to convert the points\encodedPoly into the list of latlng. Like i did using utils function.

Both functions that i used are

decodePoly :

// !DECODE POLY
  static List decodePoly(String poly) {
    var list = poly.codeUnits;
    var lList = new List();
    int index = 0;
    int len = poly.length;
    int c = 0;
    // repeating until all attributes are decoded
    do {
      var shift = 0;
      int result = 0;

      // for decoding value of one attribute
      do {
        c = list[index] - 63;
        result |= (c & 0x1F) << (shift * 5);
        index++;
        shift++;
      } while (c >= 32);
      /* if value is negative then bitwise not the value */
      if (result & 1 == 1) {
        result = ~result;
      }
      var result1 = (result >> 1) * 0.00001;
      lList.add(result1);
    } while (index < len);

    /*adding to previous value as done in encoding */
    for (var i = 2; i < lList.length; i++) lList[i] += lList[i - 2];

    print(lList.toString());

    return lList;
  }

and convertToLatLng() :

static List<LatLng> convertToLatLng(List points) {
    List<LatLng> result = <LatLng>[];
    for (int i = 0; i < points.length; i++) {
      if (i % 2 != 0) {
        result.add(LatLng(points[i - 1], points[i]));
      }
    }
    return result;
  }

This would make a exact route like i did in my app :

Here is the screenshot : ScreenShot

Upvotes: 9

Related Questions