gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:55:38 24 4
gpt4 key购买 nike

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

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

_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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com