【问题标题】:How to add Polyline on Google Maps Flutter Plugin?如何在 Google Maps Flutter 插件上添加折线?
【发布时间】:2019-04-09 19:46:56
【问题描述】:

我正在使用官方的Google Maps Flutter plugin 显示地图,它运行良好,但现在我想在地图中显示一条路线,所以我使用this package 为我提供路线,我只需要添加Polylines

【问题讨论】:

  • 那么,您检查了答案,找到了您想要的吗?

标签: dart flutter


【解决方案1】:

我用这个插件解决了同样的问题 google_maps_flutter: ^0.5.19

import 'package:google_maps_flutter/google_maps_flutter.dart';

static const LatLng _center = const LatLng(33.738045, 73.084488);
final Set<Marker> _markers = {};
final Set<Polyline>_polyline={};

//add your lat and lng where you wants to draw polyline
LatLng _lastMapPosition = _center;
List<LatLng> latlng = List();
LatLng _new = LatLng(33.738045, 73.084488);
LatLng _news = LatLng(33.567997728, 72.635997456);

latlng.add(_new);
latlng.add(_news);

//call this method on button click that will draw a polyline and markers

void _onAddMarkerButtonPressed() {
    getDistanceTime();
    setState(() {
        _markers.add(Marker(
            // This marker id can be anything that uniquely identifies each marker.
            markerId: MarkerId(_lastMapPosition.toString()),
            //_lastMapPosition is any coordinate which should be your default 
            //position when map opens up
            position: _lastMapPosition,
            infoWindow: InfoWindow(
                title: 'Really cool place',
                snippet: '5 Star Rating',
            ),
            icon: BitmapDescriptor.defaultMarker,

        ));
        _polyline.add(Polyline(
            polylineId: PolylineId(_lastMapPosition.toString()),
            visible: true,
            //latlng is List<LatLng>
            points: latlng,
            color: Colors.blue,
        ));
    });

    //in your build widget method
    GoogleMap(
    //that needs a list<Polyline>
        polylines:_polyline,
        markers: _markers,
        onMapCreated: _onMapCreated,
        myLocationEnabled:true,
        onCameraMove: _onCameraMove,
        initialCameraPosition: CameraPosition(
            target: _center,
            zoom: 11.0,
        ),

        mapType: MapType.normal,

    );
}

【讨论】:

    【解决方案2】:

    这是对 Jawad 回复的扩展答案。我用过官方的谷歌地图插件

    google_maps_flutter 0.5.19+2

    完整的工作代码如下。

    import 'package:flutter/material.dart';
    import 'package:google_maps_flutter/google_maps_flutter.dart';
    
    class TestMapPolyline extends StatefulWidget {
      @override
      _TestMapPolylineState createState() => _TestMapPolylineState();
    }
    
    class _TestMapPolylineState extends State<TestMapPolyline> {
      final Set<Marker> _markers = {};
      final Set<Polyline> _polyline = {};
    
      GoogleMapController controller;
    
      List<LatLng> latlngSegment1 = List();
      List<LatLng> latlngSegment2 = List();
      static LatLng _lat1 = LatLng(13.035606, 77.562381);
      static LatLng _lat2 = LatLng(13.070632, 77.693071);
      static LatLng _lat3 = LatLng(12.970387, 77.693621);
      static LatLng _lat4 = LatLng(12.858433, 77.575691);
      static LatLng _lat5 = LatLng(12.948029, 77.472936);
      static LatLng _lat6 = LatLng(13.069280, 77.455844);
      LatLng _lastMapPosition = _lat1;
    
      @override
      void initState() {
        super.initState();
        //line segment 1
        latlngSegment1.add(_lat1);
        latlngSegment1.add(_lat2);
        latlngSegment1.add(_lat3);
        latlngSegment1.add(_lat4);
    
        //line segment 2
        latlngSegment2.add(_lat4);
        latlngSegment2.add(_lat5);
        latlngSegment2.add(_lat6);
        latlngSegment2.add(_lat1);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GoogleMap(
            //that needs a list<Polyline>
            polylines: _polyline,
            markers: _markers,
            onMapCreated: _onMapCreated,
            initialCameraPosition: CameraPosition(
              target: _lastMapPosition,
              zoom: 11.0,
            ),
            mapType: MapType.normal,
          ),
        );
      }
    
      void _onMapCreated(GoogleMapController controllerParam) {
        setState(() {
          controller = controllerParam;
          _markers.add(Marker(
            // This marker id can be anything that uniquely identifies each marker.
            markerId: MarkerId(_lastMapPosition.toString()),
            //_lastMapPosition is any coordinate which should be your default
            //position when map opens up
            position: _lastMapPosition,
            infoWindow: InfoWindow(
              title: 'Awesome Polyline tutorial',
              snippet: 'This is a snippet',
            ),
          ));
    
          _polyline.add(Polyline(
            polylineId: PolylineId('line1'),
            visible: true,
            //latlng is List<LatLng>
            points: latlngSegment1,
            width: 2,
            color: Colors.blue,
          ));
    
          //different sections of polyline can have different colors
          _polyline.add(Polyline(
            polylineId: PolylineId('line2'),
            visible: true,
            //latlng is List<LatLng>
            points: latlngSegment2,
            width: 2,
            color: Colors.red,
          ));
        });
      }
    }
    

    【讨论】:

    • 这对我很有帮助。
    【解决方案3】:

    折线目前 (2019-01-27) 在 official google maps flutter plugin 中不可用。但是,有两个拉取请求添加了此功能:

    拉取请求 1121 包含有关如何使用折线功能的示例代码。

    编辑:添加了有关如何使用该功能的信息:

    要么等待分支 1121 被合并(如果合并)到基线中,要么克隆分支 repo。克隆后

    将 packages/google_maps_flutter 文件夹复制到与您的 Flutter 应用相同的级别,例如:

    - workspace
     |_myflutterapp
       |_lib
       |_android
       |_ios
       |_pubspec.yaml
     |_google_maps_flutter
    

    然后将您的依赖项更改为指向此版本而不是官方版本,即在您的 pubspec.yaml 文件中将其更改为:

    dev_dependencies:
      flutter_test:
        sdk: flutter
    
      google_maps_flutter:
        path: ../google_maps_flutter
    

    然后按照google flutter library上的说明操作

    google_maps_futter 包中包含一个示例,该示例准确显示了如何使用折线和抽头。

    【讨论】:

    • 您能否详细说明如何使用它?
    • @AryeeteySolomonAryeetey 我已经更新了我的答案,提供了有关如何使用此功能的更多信息。
    • @Jason 我在 google_maps_flutter 包中找不到使用折线和抽头的示例。另外我看到你是1121分支的作者,不知道什么时候合并?
    • @temirbek 我已经开始合并代码了,有点忙。将尝试在本周末完成它,并推动更改。
    【解决方案4】:

    Google Maps for Flutter 在正式版 0.5.6 https://pub.dartlang.org/packages/google_maps_flutter#056987654321@中已经支持折线

    【讨论】:

      【解决方案5】:

      您可以使用this lib

      import 'package:map_view/map_view.dart';
      import 'package:map_view/polyline.dart';
      ...
      MapView mapView = MapView();
      mapView.addPolyline(Polyline('my_polyline', [
        Location(45.52309483308097, -122.67339684069155),
        Location(45.52298442915803, -122.66339991241693),
      ]));
      

      【讨论】:

      • 您提出的库使用静态地图,不是官方插件的一部分,但是有拉取请求将此功能引入官方插件。
      猜你喜欢
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多