【发布时间】:2019-12-07 21:26:01
【问题描述】:
背景
我正在使用 google_maps_flutter 包来显示带有与房屋相对应的标记的地图。为了避免设备过载,我只在用户正在查看的位置附近装载房屋。为了简化此操作,我请求用户的位置并将其提供给 Google 地图,以便显示“我的位置”按钮。
问题
如果已授予位置访问权限,则它会按预期工作。如果地图已加载,则不会显示“我的位置”按钮。
目前,如果在屏幕上显示地图时授予位置权限,我只需将相机移动到他们的位置即可。这是一个不错的解决方法,但我宁愿让用户决定在哪里查看。
在地图已经加载的情况下获得权限后如何加载按钮?
试过
- 我试过设置状态。
- 手动将地图标记为脏。
- 使用测试方法强制地图重新加载选项。
我已经检查了我可以访问的所有可用方法,所以在这一点上我怀疑还有其他选择。如果是这种情况,我会将其报告为错误。
我的代码
@override
Widget build(BuildContext context) {
// Load markers when permission is granted.
location.hasPermission().then((currPerm) async {
if (!currPerm) {
bool newPerm = await location.requestPermission();
if (newPerm) {
LocationData data = await location.getLocation();
(await mapController.future).moveCamera(CameraUpdate.newLatLng(LatLng(data.latitude, data.longitude)));
}
}
});
GoogleMap map = GoogleMap(
initialCameraPosition: CameraPosition(
target: INIT_POS,
zoom: 11.5,
),
myLocationEnabled: true,
myLocationButtonEnabled: true,
trafficEnabled: true,
markers: markers,
onMapCreated: (newController) => mapController.complete(newController),
onCameraIdle: () async {
// Load markers when the camera stops moving.
LatLngBounds bounds = await (await mapController.future).getVisibleRegion();
loadMarkers(
LatLng(
(bounds.northeast.latitude + bounds.southwest.latitude)/2,
(bounds.northeast.longitude + bounds.southwest.longitude)/2
),
);
},
);
return map;
}
【问题讨论】:
-
没有代码,没人能回答你:'(请提供你说的代码。
-
你去。我希望这就足够了。如果需要,我绝对可以添加额外的上下文。
-
我认为这实际上是一个错误。没有理由不工作。
-
不管用户第一次授予权限,为什么不触发moveCamera?
-
也许这就是呈现您想要的按钮的原因
标签: google-maps flutter