Google 地图需要花费大量时间来加载 Flutter 中的当前位置

N.K*_*K.T 5 google-maps flutter google-maps-flutter

我正在尝试打开带有当前位置的谷歌地图。为此,我使用有状态的小部件。但地图需要很长时间才能加载,有时甚至根本无法加载。这是因为Future函数(用于获取当前位置)还是我的实现错误?我正在使用google_maps_flutter地图插件。这是我的代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          FutureBuilder(
            future: _getCurrentLocation(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                print("map loaded");
                Placemark placemark = snapshot.data;
                var json = placemark.toJson();
                selectedLocation = TheLocation.fromJson(json);
                _markers.add(
                  Marker(
                    markerId: MarkerId('homeMarker'),
                    position: LatLng(
                        selectedLocation.latitude, selectedLocation.longitude),
                    icon: BitmapDescriptor.defaultMarker,
                    infoWindow: InfoWindow(title: selectedLocation.name),
                    draggable: false,
                  ),
                );
                return GoogleMap(
                  initialCameraPosition: CameraPosition(
                    target: LatLng(placemark.position.latitude,
                        placemark.position.longitude),
                    zoom: 18,
                  ),
                  markers: _markers,
                  onMapCreated: onMapCreated,
                  onCameraMove: ((_position) => _updatePosition(_position)),
                );
              } else {
                print("loading map");
                return Container(
                  child: Center(
                    child: Text("Loading map. Please Wait ... ..."),
                  ),
                );
              }
            },
          ),
        ],
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

使用地理定位器插件获取当前位置:

Future<Placemark> _getCurrentLocation() async {
    List<Placemark> placeMarks;
    await geolocator
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((Position position) async {
      placeMarks = await Geolocator().placemarkFromCoordinates(
        position.latitude,
        position.longitude,
        localeIdentifier: "en_US",
      );
    }).catchError((e) {
      print("Location error $e");
    });
    return placeMarks[0];
  }
Run Code Online (Sandbox Code Playgroud)

onMapCreated功能 :

void onMapCreated(GoogleMapController controller) {
    setState(() {
      googleMapController = controller;
    });
  }
Run Code Online (Sandbox Code Playgroud)

Sis*_*sir 4

似乎这是一个已知问题。检查此Git 问题链接。正如最后评论中所建议的,作为一种解决方法,您可以创建一个FutureBuilder模拟延迟作为未来,然后使用进度指示器来显示加载。