如何在两个标记之间的Google地图上绘制道路?

时间:2019-07-14 15:34:36

标签: google-maps flutter

我正在扑扑应用程序,需要在两个标记之间画一条路。如何做到这一点?

我尝试了this片段,但是代码有些复杂。还有没有其他更简单的方法可以通过Google地图来完成此操作而不使用地图视图?

第一个标记是用户的位置:

var currentLocation;
  void _onAddMarkerButtonPressed() async{
    var location = new loc.Location();
    try {
      currentLocation = await location.getLocation();
      _center = LatLng(currentLocation.latitude, currentLocation.longitude);
      setState(() {
        _markers.add(Marker(
          markerId: MarkerId(LatLng(currentLocation.latitude, currentLocation.longitude).toString()),
          position: LatLng(currentLocation.latitude, currentLocation.longitude),
          infoWindow: InfoWindow(
            title: 'Your locate',
          ),

          icon: BitmapDescriptor.fromAsset("images/google-map-pointer-grey-th.png"),
        ));
      });
    } on Exception {
      currentLocation = null;
    }
  }

第二个标记是他输入的地址:

RaisedButton(  onPressed: () async {
                Prediction p = await PlacesAutocomplete.show(
                  context: context, apiKey: kGoogleApiKey,
                  mode: Mode.overlay
                );
                displayPrediction(p);
              },)
Future displayPrediction(Prediction p) async {
    if (p != null) {
      print(p);
      PlacesDetailsResponse detail =
      await _places.getDetailsByPlaceId(p.placeId);

      var placeId = p.placeId;
      String name = detail.result.name;
      double lat = detail.result.geometry.location.lat;
      double lng = detail.result.geometry.location.lng;

     // var address = await Geocoder.local.findAddressesFromQuery(p.description);

      print(name);
      print(lat);
      print(lng);
    }}

我的Google地图:

final Set<Marker> _markers = {};
  var _center = const LatLng(45.521563, -122.677433);
  Completer<GoogleMapController> _controller = Completer();

  Widget googleMap(BuildContext context) {
    return Container(
      child: GoogleMap(
        markers: _markers,
        mapType: MapType.normal,
        initialCameraPosition: CameraPosition(
          target: _center,
          zoom: 20.0,
        ),
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
    );
  }

0 个答案:

没有答案