如何使用Flutter获取真实的方向指示? (绘制真实路线)

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

我用这种方式:Linq All

<Link to="/DashboardH" classname="w3-bar-item w3-button">History </Link>

我的输出看起来像这样:

https://developers.google.com/maps/documentation/directions/intro#ExampleRequests

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

如何使用Flutter获取真实的方向指示? (绘制真实路线)

c38357868 回答:如何使用Flutter获取真实的方向指示? (绘制真实路线)

要找到确切的路线,您必须从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"];
  }

您将获得一个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

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

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

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

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

我使用的两个功能都是

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;
  }

和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;
  }

这将像我在应用程序中一样进行精确的路由:

这是屏幕截图: ScreenShot

本文链接:https://www.f2er.com/3112583.html

大家都在问