简体   繁体   English

Flutter Google 地图在首次启动时不显示位置

[英]Flutter Google Maps doesn't show location on first launch

I'm using Google Maps (google_maps_flutter: ^2.1.3) in my Flutter app, and when I open the app on the phone for the first time, it asks for location permission.我在我的 Flutter 应用程序中使用谷歌地图 (google_maps_flutter: ^2.1.3),当我第一次在手机上打开该应用程序时,它会询问位置权限。 After giving this permission, my location on the Map is not shown with a blue dot and the go to my location button is not visible either.授予此权限后,我在 Map 上的位置未显示为蓝点,并且我的位置按钮的 go 也不可见。 However, after that, when I close the application and open it again, both my location appears with a blue dot and the go to my location button is actively displayed on the screen.但是,在那之后,当我关闭应用程序并再次打开它时,我的位置都出现了一个蓝点,并且 go 到我的位置按钮主动显示在屏幕上。

Here is my GoogleMap widget:这是我的 GoogleMap 小部件:

GoogleMap(     
                          myLocationEnabled: true,
                          myLocationButtonEnabled: true,
                          mapType: MapType.normal,
                          initialCameraPosition: const CameraPosition(
                            target: LatLng(30, 30),
                            zoom: 4,
                          ),
                          markers: markers,
                          mapToolbarEnabled: false,
                        ),

What do I need to do in order for my location to appear on Google Maps after I give location permission when I first open the application?在我首次打开应用程序时授予位置权限后,我需要做什么才能让我的位置出现在 Google 地图上?

I was initially launching my app from HomePage but to fix this issue I created the transition page LandingPage.我最初是从 HomePage 启动我的应用程序,但为了解决这个问题,我创建了转换页面 LandingPage。 In LandingPage, I first checked if the user's location is turned on.在 LandingPage 中,我首先检查了用户的位置是否打开。 I forced the user to turn on their location if it wasn't turned on.如果没有打开,我强迫用户打开他们的位置。 If it's turned on, I got the user's location and sent that location to HomePage using constructor.如果它已打开,我会获取用户的位置并使用构造函数将该位置发送到 HomePage。 In HomePage, I started the map from this location.在主页中,我从这个位置启动了 map。 Thus, the user gave the location permission on the previous transition page and I used the location we got on that page as the map starting location.因此,用户在上一个转换页面上授予了位置权限,我使用我们在该页面上获得的位置作为 map 起始位置。

I performed the following operations on LandingPage:我在 LandingPage 上执行了以下操作:

bool isLocationEnabled = false;
  bool isLocationEnabledChecked = false;
  double? latMe;
  double? longMe;
  Location _locationTracker = Location();

  Future<void> checkIsLocationEnabled() async {
    isLocationEnabled = await _locationTracker.serviceEnabled();
    setState(() {});
    if (isLocationEnabled) {
      var location = await _locationTracker.getLocation();
      latMe = location.latitude;
      longMe = location.longitude;
    }
    setState(() {
      isLocationEnabledChecked = true;
    });
  }


@override
  void initState() {
    super.initState();
    checkIsLocationEnabled();
  } 

@override
  Widget build(BuildContext context) {
      if (isLocationEnabledChecked) {
        if (isLocationEnabled) {
          return HomePage(
            latitude: latMe,
            longitude: longMe,
          );
        } else {
          return EnableLocationScreen();
        }
      } else {
        return WaitingScreen();
      }
  }

I performed the following operations on HomePage:我在主页上执行了以下操作:

GoogleMap(
                                onMapCreated: (GoogleMapController controller) async {
                                  _customInfoWindowController.googleMapController = controller;
                                  mapController.complete(controller);
                                },
                                onTap: (position) {
                                  _customInfoWindowController.hideInfoWindow!();
                                },
                                onCameraMove: (position) {
                                  _customInfoWindowController.onCameraMove!();
                                },
                                myLocationEnabled: true,
                                myLocationButtonEnabled: true,
                                mapType: MapType.normal,
                                initialCameraPosition: CameraPosition(
                                  target: LatLng(widget.latitude ?? 38.917001095328345, widget.longitude ?? 35.532376170158386),
                                  zoom: 15,
                                ),
                                markers: markers,
                                polygons: polygon,
                                mapToolbarEnabled: false,
                                minMaxZoomPreference: const MinMaxZoomPreference(10, 20),
                              ),

I am using location: ^4.3.0 package.我正在使用位置:^4.3.0 package。

In IOS it works and in android it needs reboot so show permission on splash before opening map page or after getting permission reload page.在 IOS 中它可以工作,在 android 中它需要重新启动,因此在打开 map 页面之前或在获得权限重新加载页面之后显示启动权限。 Simple solution set bool isLocationEnabled=false;简单的解决方案 set bool isLocationEnabled=false; then when you get permission from user make然后当您获得用户 make 的许可时

setState(() {
      isLocationEnabled=true;
    }); 

and set the value as follows并将值设置如下

GoogleMap(     
                      myLocationEnabled: isLocationEnabled,
                      myLocationButtonEnabled: true,
                      mapType: MapType.normal,
                      initialCameraPosition: const CameraPosition(
                        target: LatLng(30, 30),
                        zoom: 4,
                      ),
                      markers: markers,
                      mapToolbarEnabled: false,
                    ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM