我在屏幕上绘制了点,它们的位置基于偏移量,我正在尝试将此偏移量转换为响应 googleMap 上的 LatLng。
您知道活动是否可以举办吗?我需要这个,因为我正在尝试使用触摸手势添加绘图多边形。
我尝试使用 GoogleMapController 中的此功能:
Future<LatLng> getLatLng(ScreenCoordinate screenCoordinate) { return GoogleMapsFlutterPlatform.instance .getLatLng(screenCoordinate, mapId: mapId); }
但似乎ScreenCoordinate是整个地图的坐标,它与Offset不符。
请记住,纬度和经度的值范围为 -90 到 90(纬度)和 -180 到 180(经度)。
要将偏移量转换为 LatLng:
extension LatLngExt on Offset{
LatLng get getLatLng{
if((dx > -90 && dx < 90) && (dy > -180 && dy < 180))
{
return LatLng(dx,dy);
}
throw ArgumentException('Invalid Offset');
}
}
此 getter 方法返回
LatLng
对象 当且仅当 该偏移量具有可分配给 dx , dy
的有效 LatLng
值。否则会抛出 ArgumentException
,你必须处理它。
创建多边形时,只需从偏移量处调用该 getter 即可:
myOffset.getLatLng