此函数为CameraPosition中的zoom参数返回null而不是正确的值,我无法弄明白
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了此函数为CameraPosition中的zoom参数返回null而不是正确的值,我无法弄明白相关的知识,希望对你有一定的参考价值。
我有一个函数,根据2个Google地图标记之间的距离计算所需的CameraPosition缩放量。调用CameraPosition缩放参数(zoomIn)中指定的方法,该方法又调用另一个异步方法,该方法返回两点之间的距离(getDistance)。两种方法都正常运行,但尽管print语句显示正确的计算值,但zoomIn始终返回null。这是同步问题吗?
@override
Widget build(BuildContext context)
return Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
onMapCreated: addMarkers,
cameraTargetBounds: CameraTargetBounds(createTargetBounds()),
initialCameraPosition: CameraPosition(
target:
LatLng((currentLocation.latitude + latitudesArr[selectedDestination]) / 2,
(currentLocation.longitude + longitudesArr[selectedDestination]) / 2),
zoom: zoomIn(bounds: createTargetBounds(), padding: 200.0)),
),
)
],
);
double zoomIn(LatLngBounds bounds, double padding)
double zoom;
getDistance(bounds).then((double distance)
print('Distance: $distance');
if (distance > 200)
zoom = 16.0;
else
zoom = 18.0;
print ('Zoom: $zoom');
);
return zoom;
Future<double> getDistance(LatLngBounds bounds) async
double distance = await Geolocator().distanceBetween(
bounds.southwest.latitude,
bounds.southwest.longitude,
bounds.northeast.latitude,
bounds.northeast.longitude);
print('Distance: $distance');
return distance;
The zoomIn function correctly populates the zoom variable with 16.0 or 18.0 depending on the distance between the two markers. However, null is always returned.
答案
好吧,我认为这样做的正确方法是在声明Google地图窗口小部件时对缩放值进行硬编码,例如:
class MapState extends State<MapRoute>
@override
Widget build(BuildContext context)
return Stack(
children: <Widget>[
GoogleMap(
onMapCreated: addMarkers,
myLocationEnabled: true,
cameraTargetBounds: CameraTargetBounds(createTargetBounds()),
initialCameraPosition: CameraPosition(
target:
LatLng((currentLocation.latitude + latitudesArr[selectedDestination]) / 2,
(currentLocation.longitude + longitudesArr[selectedDestination]) / 2),
zoom: 18.0),
),
然后,在这个例子中,在addMarkers
方法上,下面的代码将一直等到GeoLocator
返回一个距离,然后才能确定所需的缩放级别。之后只是调用地图控制器的moveCamera(CameraUpdate.zoomTo(zoom))
如下:
await Geolocator().distanceBetween(
bounds.southwest.latitude,
bounds.southwest.longitude,
bounds.northeast.latitude,
bounds.northeast.longitude)
.then((distance)
print('The Distance: $distance');
double zoom;
if (distance > 300)
zoom = 17.0;
else if (distance > 100)
zoom = 18.0;
else
zoom = 19.0;
print ('Zoom: $zoom');
controller.moveCamera(CameraUpdate.zoomTo(zoom));
);
以上是关于此函数为CameraPosition中的zoom参数返回null而不是正确的值,我无法弄明白的主要内容,如果未能解决你的问题,请参考以下文章
2021-02-07 Python scipy ndimage.zoom()函数
使用 GoogleMap 带边界的 CameraPosition