Flutter:如何从谷歌地图中清除折线

标签 flutter dart google-maps-flutter

我用下面的代码创建了一个带有标记和折线的谷歌地图小部件。

一切正常,除了当我在两个坐标之间绘制一条折线,然后尝试删除它时,折线不会被删除(至少不会永久删除)。当我使用以下方法尝试删除旧折线并添加新折线时,我得到两条折线:

_polyLine.clear()
_polyLine.remove("id")
polylines.removeWhere((m) => m.polylineId.value == 'polyId$_randPolyId')

除了新的之外,它被删除然后再次添加。结果如下图:

The Results
...
void showPinsOnMap() {        
      // _polylines.removeWhere(
      // (m) => m.polylineId.value == 'polyId$_randPolyId');
      // _polylines.clear()
      _polylines = {};
      print("\n\n\nPolyLines Lenght: ${_polylines.length}\n\n\n");

   var pinPosition = LatLng(_currentLocation.latitude,_currentLocation.longitude);

   var destPosition = LatLng(_destinationLocation.latitude,
   _destinationLocation.longitude);
   _markers.add(Marker(
      markerId: MarkerId('sourcePin'),
      position: pinPosition,
      icon: _sourceIcon
   ));
   _markers.add(Marker(
      markerId: MarkerId('destPin'),
      position: destPosition,
      icon: _destinationIcon,
   ));

   setPolylines();
   notifyListeners();
}

...
...
void setPolylines() async {
   List<PointLatLng> result = await _polylinePoints.getRouteBetweenCoordinates(
   _googleAPIKey,
   _currentLocation.latitude,
   _currentLocation.longitude,
   _destinationLocation.latitude,
   _destinationLocation.longitude).catchError((onError){
     print("\n\n\n An Error Occured Possibly dure to network issues\n\n\n");
   });

  if(result != null){
    print("Results not null");
   if(result.isNotEmpty){
      result.forEach((PointLatLng point){
         _polylineCoordinates.add(
            LatLng(point.latitude,point.longitude)
         );
      });
       _isLoadingData = false;
      _polylines.add(Polyline(
        width: 5, 
        polylineId: PolylineId('polyId$_randPolyId'),
        color:
        Color.fromARGB(255, 40, 122, 198), 
        points: _polylineCoordinates
        ));
  }
  }
  notifyListeners();
}
...

最佳答案

您需要在绘制新路径之前清除 polylineCoordinates。

polylineCoordinates.clear();//在检查结果值之前调用它。

polylineCoordinates.clear();
    if(result != null){
    print("Results not null");
   if(result.isNotEmpty){
      result.forEach((PointLatLng point){
         _polylineCoordinates.add(
            LatLng(point.latitude,point.longitude)
         );
      });

  }
}

关于Flutter:如何从谷歌地图中清除折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60402999/

相关文章:

android - 从后台恢复后,Flutter GoogleMap 为空白

flutter - 如何设置 google_maps_flutter 的缩放级别

ios - 在 iOS 上更新 map 样式时白色闪烁

flutter 2 : problem in Intl null safety library

flutter - 滑动时向 PageView 添加新页面

flutter sqflite : Database unstable after hot reload

flutter - 从Flutter的下拉菜单中删除项目会导致错误

android - 用于嵌套导航的 Flutter WillPopScope

dart - 检查无状态小部件是否在 flutter 中处理

flutter - 通过模型生成的按钮处理onPress