如何解决Google Maps Flutter 无法在 iOS 上正确显示多边形
我正在使用 google_maps_Flutter 在谷歌地图上显示多边形。 每次用户点击地图时,都会将一个点添加到点列表中,然后将其输入到我创建多边形的方法中。 我还有一个删除按钮,可以清除我的点列表,因此我的基于这些点的多边形也会被清除(仅在 ANDROID 上)。
以下是我认为与问题相关的代码段:
List<LatLng> polygonCoords = [LatLng(0,0),LatLng(0,0)]; // this list holds the points that // form my polygon
void _setCoords(latlong) { // latlong are the coordinats received from the user tap on the map
setState(() {
LatLng coords = latlong;
if (polygonCoords.lastIndexOf(zero) == 1) { // where zero = LatLng(0,0)
polygonCoords.removeWhere((element) => element == zero);
polygonCoords.add(coords);
polygonCoords.add(coords); // first coordinate is added twice because the polygon needs // to loop back to the first point to close
} else {
polygonCoords.removeLast();
polygonCoords.add(coords);
}
});
}
Set<polygon> mypolygon() {
Set<polygon> polygonSet = new Set();
polygonSet.add(polygon(
fillColor: Colors.white,polygonId: polygonId('polygonId'),points: polygonCoords,strokeWidth: 3,strokeColor: Colors.white,));
return polygonSet;
}
GoogleMap(
polygons: mypolygon(),// here I give the polygon set to my map
mapType: MapType.satellite,initialCameraPosition: CameraPosition(
target: snapshot.data,zoom: 16.0,),onMapCreated: _onMapCreated,onTap: (latlong) {
_setCoords(latlong); // calling the method above to add the coordinates to my list
},);
void deleteSelection() {
setState(() {
polygonCoords.clear();
polygonCoords.add(zero);
polygonCoords.add(zero);
});
}
iOS 上发生的情况如下: 我在地图上添加了 4 个点。我按下删除按钮,没有任何反应(我检查了日志,列表确实清除了)。我在地图上再放一个点,之前的多边形还在显示,然后我再添加一个点,没有任何变化。当我添加第三个点时,旧的多边形消失,新的多边形呈现在我的 UI 中。
我尝试在 deleteSelection() 方法内调用 mypolygon(),尝试在方法外声明集合,尝试清除集合。没有任何变化,在 Android 上它按预期工作,但在 iOS 上,即使我检查过并且多边形确实有新坐标,它仍然不会显示它,直到有 3 个点。
我现在有一个解决方案是当我的集合中少于 3 个点 (visible: polygonCoords.length < 3 ? false : true
) 时,将多边形的可见属性设置为 false,但我想知道为什么上述方法不不适用于 iOS,以及为什么在 Android 上它显示创建一条线的前 2 个点。
使用 google_maps_Flutter:^1.1.0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。