【问题标题】:Reload My Location button when permission is granted授予权限后重新加载我的位置按钮
【发布时间】:2019-12-07 21:26:01
【问题描述】:

背景

我正在使用 google_maps_flutter 包来显示带有与房屋相对应的标记的地图。为了避免设备过载,我只在用户正在查看的位置附近装载房屋。为了简化此操作,我请求用户的位置并将其提供给 Google 地图,以便显示“我的位置”按钮。

问题

如果已授予位置访问权限,则它会按预期工作。如果地图已加载,则不会显示“我的位置”按钮。

目前,如果在屏幕上显示地图时授予位置权限,我只需将相机移动到他们的位置即可。这是一个不错的解决方法,但我宁愿让用户决定在哪里查看。

在地图已经加载的情况下获得权限后如何加载按钮?

试过

  1. 我试过设置状态。
  2. 手动将地图标记为脏。
  3. 使用测试方法强制地图重新加载选项。

我已经检查了我可以访问的所有可用方法,所以在这一点上我怀疑还有其他选择。如果是这种情况,我会将其报告为错误。

我的代码

  @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


【解决方案1】:

截至google_maps_flutter: ^0.5.23+1,我通过将变量分配给myLocationEnabled 并在获得位置权限后更改其值来解决它。如果你硬编码true,它就行不通了。

GoogleMap map = GoogleMap(
      myLocationEnabled: _isLocationGranted,
      ...

所以,_isLocationGranted 最初是 false。当我获得位置权限时,我将其设置为true 并重建小部件(通过调用setState()、触发BlocBuilder() 或您使用的任何方法)。

另一个相关参数myLocationButtonEnabled: true 似乎不需要这种处理。

【讨论】:

  • 我也遇到了同样的问题,你能举个例子吗?
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 2011-03-16
  • 1970-01-01
  • 2022-06-28
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多