[英]Unable to get latitude and longitude user - Flutter
I am using google_maps_flutter: ^2.2.0 and geolocator: ^9.0.1 and trying to get user latitude and longitude by running this code from geolocator package:我正在使用 google_maps_flutter: ^2.2.0 和 geolocator: ^9.0.1 并尝试通过从地理定位器 package 运行此代码来获取用户纬度和经度:
Position posisiNow = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
The problem is... I always getting error when trying to reach posisiNow.latitude
or posisiNow.longitude
but when I only print posisiNow
I get good result: Latitude: 37.33233141, Longitude: -122.0312186
.问题是...尝试到达posisiNow.latitude
或posisiNow.longitude
时总是出错,但是当我只打印posisiNow
时,我得到了很好的结果: Latitude: 37.33233141, Longitude: -122.0312186
。 The error when I try to print posisiNow.latitude
is: NoSuchMethodError: The getter 'latitude' was called on null.
我尝试打印posisiNow.latitude
时的错误是: NoSuchMethodError: The getter 'latitude' was called on null.
I need that data because I need to put latitude and longitude in this function of google_maps_flutter:我需要这些数据,因为我需要在 google_maps_flutter 的 function 中输入纬度和经度:
controllerNow.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 192.8334901395799,
target: LatLng(posisiNow.latitude, posisiNow.longitude),
tilt: 0,
zoom: 16.00)));
This problem only in ios Simulator, I have tried in android and worked fine.此问题仅在 ios 模拟器中,我在 android 中尝试过并且工作正常。 I also open this question in https://github.com/Baseflow/flutter-geolocator/issues/1135我也在https://github.com/Baseflow/flutter-geolocator/issues/1135打开这个问题
Here target: LatLng(posisiNow.latitude, posisiNow.longitude),
the latitude and longitude cannot be null.这里的target: LatLng(posisiNow.latitude, posisiNow.longitude),
经纬度不能是null。 You can either provide default fallback values.您可以提供默认的后备值。 target: LatLng(posisiNow.latitude?? 0.0, posisiNow.longitude?? 0.0), you need to make Position?
目标:LatLng(posisiNow.latitude?? 0.0, posisiNow.longitude?? 0.0),需要制作Position?
nullable.可以为空。
Also you can get location before building this widget.您还可以在构建此小部件之前获取位置。 eg in init state.例如在初始化 state 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.