【问题标题】:didn't get current location [closed]没有得到当前位置[关闭]
【发布时间】:2020-10-08 12:26:29
【问题描述】:

我试图通过以下代码通过地理定位器包获取当前位置

 GoogleMapController mapController;
  Position _currentPosition;
  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(30.033333, 31.233334), // egypt
    zoom: 5.4746,
  );
  

这是应该转到当前位置的按钮功能

onTap: () {
                          mapController.animateCamera(
                            CameraUpdate.newCameraPosition(
                              CameraPosition(
                                target: LatLng(
                                  _currentPosition.latitude,
                                  _currentPosition.longitude,
                                ),
                                zoom: 18.0,
                              ),
                            ),
                          );
                        },

【问题讨论】:

  • 请提供代码你如何获得_currentPosition变量,我猜你没有在任何地方分配它
  • 我尝试了下面的答案,但它给了我一个错误的位置!!

标签: google-maps flutter


【解决方案1】:

通过查看 Geolocator 上的 ReadMe,我可能会这样做。我自己没有测试过。

onTap: () async {
_currentPosition = await getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
                      mapController.animateCamera(
                        CameraUpdate.newCameraPosition(
                          CameraPosition(
                            target: LatLng(
                              _currentPosition.latitude,
                              _currentPosition.longitude,
                            ),
                            zoom: 18.0,
                          ),
                        ),
                      );
                    },

在我看来,您似乎没有为 _currentPosition 分配任何值,只是说它是 Position 类型,因此您得到一个空结果

【讨论】:

  • 它给了我一个错误的位置!!
  • 如果您运行的是模拟器,那么它不会提供您的位置,而是提供模拟器位置,如果您没有更改模拟器位置,则通常是 Android 上的 Googleplex。
  • 我将 Googleplex(初始相机位置)更改为埃及,所以当我点击我的位置按钮时,会转到不同的地方“美国”
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多