简体   繁体   English

如何在Flutter上的两个地理位置之间绘制路线?

[英]How can I draw a route between two geopoints on the Flutter?

How can I draw a route between two geopoints on the Flutter with the new widget Google Maps? 如何使用新的Google Maps小工具在Flutter上的两个地理位置之间绘制路线? There were some tutorials about how to do this with the previous widget, but now it's deprecated. 有一些关于如何使用先前的小部件执行此操作的教程,但现在已弃用。

There are many ways of implementing it. 有很多实现方法。 This is how I did it few weeks ago. 这是我几周前所做的。 First i had these plugins: 首先,我有这些插件:

import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

next create your GoogleMap object: 接下来创建您的GoogleMap对象:

LatLng izp = LatLng(-3.386943, 29.371636);

  GoogleMap googleMap = GoogleMap(
      initialCameraPosition: CameraPosition(target: izp, zoom: 10.0),
      compassEnabled: true,
      scrollGesturesEnabled: true,
      zoomGesturesEnabled: true,
      onMapCreated: (g) {
        gmc = g;//for mapcontroller if any
      },
      markers: {},
      polylines: {},//initialize the polylines list
    );

From Google Directions, fetch directions between your two points. 通过Google路线,获取两点之间的路线。 Decode the result and get points. 解码结果并获得分数。 should be at this position: 应该在这个位置:

dynamic directions = decodedResult["routes"][0]["overview_polyline"]['points'];

Create a PolylinePoints object and decode your directions object: 创建一个PolylinePoints对象并解码您的路线对象:

PolylinePoints points = new PolylinePoints();

List<PointLatLng> result = points.decodePolyline(directions);

Create a list of LatLng from the list of PointLatLng : PointLatLng的列表中创建LatLng的列表:

List<LatLng> po = [];
      result.forEach((f) {
        po.add(LatLng(f.latitude, f.longitude));
      });

Then with that list of LatLng , create a polyline object: 然后使用LatLng列表创建一个折线对象:

 Polyline route = new Polyline(
          polylineId: PolylineId("route"),
          geodesic: true,
          points: po,
          width: 20,
          color: Colors.blue);

Then after set the polyline object to the your google map object: 然后在将折线对象设置为您的Google Map对象之后:

 setState(() {
 //if you created markers, add them too if you wish
        googleMap.markers.add(marker);
        googleMap.markers.add(marker2);
        googleMap.polylines.add(route);

        /*if you associated a googlemapcontroller, you can do further actions like zooming...*/
      });

I think this should help. 我认为这应该有所帮助。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM