如何使用 Flutter 获得真正的方向步骤?(绘制真实路线)

Blo*_*oss 1 google-maps google-maps-api-3 dart flutter

我正在使用出租车应用程序。是否可以使用颤振在真实道路上绘制真实路线?

我用这种方式: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());
  }
Run Code Online (Sandbox Code Playgroud)

我的输出是这样的:

在此处输入图片说明

但我需要这样:(在真实道路上绘制目的地路线)

在此处输入图片说明

Sah*_*ngh 6

要找到您必须从方向 API 中的 json 中points退出的确切路线polylineoverview

这就是我提取确切路线的方式,就像您在第二张图片中显示的那样。

它是一个将点作为字符串返回的函数

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"];
  }
Run Code Online (Sandbox Code Playgroud)

你会得到一串points与此类似的字符串

 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
Run Code Online (Sandbox Code Playgroud)

现在,您将像这样在多段线集中添加多段线

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

encodedPoly是从以前的方法中提取的相同字符串。

在上面的函数中,您必须将 转换points\encodedPoly为 latlng 列表。就像我使用 utils 函数一样。

我使用的两个功能都是

解码聚:

// !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;
  }
Run Code Online (Sandbox Code Playgroud)

和 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;
  }
Run Code Online (Sandbox Code Playgroud)

这将像我在我的应用程序中所做的那样制作一个精确的路线:

这是屏幕截图: 截屏