简体   繁体   English

无法获取经纬度用户-Flutter

[英]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.latitudeposisiNow.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.

相关问题 如何在颤振中获得纬度和经度 - How to get latitude and longitude in flutter 如何在颤振地图上获取标记纬度和经度? - How to get marker latitude and longitude on flutter map? 如何在颤振应用程序中集成地图并获取用户在地图上设置的标记纬度和经度? - How to Integrate Map in flutter Application and get marker latitude and longitude which user set on map? 如何使用点击地点的纬度和经度获取 Flutter 中地点的自动完成位置? - How to get autocomplete location of places in Flutter with Latitude and Longitude of tapped place? Flutter 如何从 api 响应中获取所有经纬度 - Flutter how to get all latitude and longitude from api response 如何在 google_maps_flutter 中获取当前的纬度和经度? - How to get current latitude & longitude in google_maps_flutter? 在 flutter 中禁用 Internet 时获取我所在位置的纬度和经度 - get latitude and longitude of my location when internet is disable in flutter Flutter 中的纬度和经度为空(获取当前位置) - Latitude and Longitude is getting null in Flutter (Get Current Location) Flutter 如何使用地理定位器 package 获取纬度、经度? - Flutter how to get latitude, longitude using geolocator package? Flutter 获取位置和经纬度 - Flutter fetch Location and Latitude and Longitude
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM