【问题标题】:how to get real direction steps using Flutter? (draw real route)如何使用 Flutter 获得真正的方向步骤? (绘制真实路线)
【发布时间】:2019-11-13 05:37:45
【问题描述】:

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

我是这样用的: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());
  }

我的输出如下所示:

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

【问题讨论】:

  • 我发现很难理解实际问题是什么。请您详细说明一下。
  • @SahdeepSingh 我更新了我的问题。请检查..我添加了另一张照片..请检查

标签: google-maps flutter google-maps-api-3 dart


【解决方案1】:

要找到确切的路线,您必须从方向 api 的 json 中从 polylineoverview 中获取 points

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

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

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

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

这是截图:

【讨论】:

  • 如果我想获取编码折线的位置列表,如何使用上面的代码获取它?
  • @gsm 您需要将编码的折线转换为点列表,从点中您可以获得 LatLng 列表。获得这些的两种方法(第一点,然后是 latlng 列表)都在答案中。
  • @SahdeepSingh 你好,我想在用户转错时改变我的折线,就像谷歌地图应用程序正在做的那样。你能帮忙吗?
  • @ShriyaPandya 您可能想要管理您的出发地和目的地状态。并使用实时位置,您可以更改原点变量并在清除先前的折线后重新运行获取路线的整个过程。
  • @SahdeepSingh 是的,我也在做同样的事情。在更新用户实时位置时,首先清除折线列表,然后将实时位置 latllng 更新为源 latlng。但是这样,折线每秒都会在地图上闪烁。当我穿过马路时,折线停止显示。因为那个时候实时位置变化很快。
猜你喜欢
  • 1970-01-01
  • 2011-09-20
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 2012-08-14
  • 2011-01-01
相关资源
最近更新 更多