【问题标题】:Flutter - Google Maps doesn´t wait to LocationFlutter - 谷歌地图不等待位置
【发布时间】:2019-10-06 20:04:12
【问题描述】:

我正在尝试显示当前位置的地图。为此,我使用了 LocationGoogle Map 插件(最新版本)。

我有一个类似的代码:

var lng, lat;

          @override
          initState() {
            super.initState();
            loading = true;
            getLocation();
          }

              Future getLocation() async {
                    final location = Location();
                    var currentLocation = await location.getLocation();
                    setState(() {
                      lat = currentLocation.latitude;
                      lng = currentLocation.longitude;
                      loading=false;
                    });
                  }

             loading==false ? GoogleMap(
                 mapType: MapType.hybrid,
                 myLocationButtonEnabled: true,
                 myLocationEnabled: true,
                 initialCameraPosition: CameraPosition(
                      target: LatLng(lat, lng),
                      zoom: 15.0,
                            )):null,

当我们使用地图查看视图时,会在 1 秒左右出现错误(然后视图加载正确),并在控制台中显示此错误

I/flutter (15567):══╡ 小部件库发现异常 ╞═════════════════════════════════════════════════ ══════════ I/颤振 (15567):以下断言被抛出构建 HomePageScreen(脏,依赖项:I/flutter(15567): [_LocalizationsScope-[GlobalKey#78c30],MediaQuery],状态: _HomePageScreen#9c9d2): I/flutter (15567): 'package:google_maps_flutter/src/location.dart': 断言失败: 第 17 行 pos 16: '纬度!= I/flutter (15567): null': 不正确。

我调试了一下,错误比较简单:Location插件加载经纬度慢,Google Maps插件加载更快。所以我们有一个错误。

问题是:如何强制谷歌地图等待位置插件的位置和经度?

【问题讨论】:

  • 定义等待:是隐藏地图还是有默认点?
  • 我想在加载当前位置时隐藏它。默认点是当前位置(借助 Location 插件)

标签: google-maps flutter


【解决方案1】:

您可以在获得您的位置之前显示加载程序,

var lng, lat;

      @override
      initState() {
        super.initState();
        loading = true;
        getLocation();
      }

          Future getLocation() async {
                final location = Location();
                var currentLocation = await location.getLocation();
                setState(() {
                  lat = currentLocation.latitude;
                  lng = currentLocation.longitude;
                  loading=false;
                });
              }

         loading==false ? GoogleMap(
             mapType: MapType.hybrid,
             myLocationButtonEnabled: true,
             myLocationEnabled: true,
             initialCameraPosition: CameraPosition(
                  target: LatLng(lat, lng),
                  zoom: 15.0,
                        )):CircularProgressIndicator(),

【讨论】:

  • 你确定loading是假的,lat和lng不为空吗?
  • 您好,我已经尝试过您的答案,但var currentLocation = await location.getLocation(); 给出了问题,我该如何解决?它给出了 getLocation isnt define type Location 你能帮我吗
【解决方案2】:

当您的latlng 为空时,显示一个空的Container() 或任何加载指示器。

lat == null || lng == null
  ? Container()
  : GoogleMap(
      mapType: MapType.hybrid,
      myLocationButtonEnabled: true,
      myLocationEnabled: true,
      initialCameraPosition: CameraPosition(
        target: LatLng(lat, lng),
        zoom: 15.0,
      ),
    );

【讨论】:

    【解决方案3】:

    你也可以试试这个,去掉空的Container

    GoogleMap(
          mapType: MapType.hybrid,
          myLocationButtonEnabled: true,
          myLocationEnabled: true,
          initialCameraPosition: CameraPosition(
            target: LatLng(lat ?? 0, lng ?? 0), // so if lat, lng is null, we use 0 as placeholder. 
            zoom: 15.0,
          ),
        )
    

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 1970-01-01
      • 2023-01-25
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      相关资源
      最近更新 更多