如何在Flutter中获取用户位置

uttx8gqw  于 2023-03-24  发布在  Flutter
关注(0)|答案(5)|浏览(198)

我想定位用户并通过API将其位置坐标发送到服务器,请用代码回答,我已经尝试了库geolocator:第7.7.0章

void getCurrentLocation() async {
     Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
     var lat = position.latitude;
     var long = position.longitude;
 print("Latitude: $lat and Longitude: $long");
   }

此代码不起作用,错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator)
E/flutter (25476): #0      MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:156
E/flutter (25476): <asynchronous suspension>
E/flutter (25476): #1      MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:128:27)
E/flutter (25476): <asynchronous suspension>
E/flutter (25476): #2      getCurrentLocation
package:check_time/Screens/fingerprint_Screen.dart:84
E/flutter (25476): <asynchronous suspension>
ep6jt1vc

ep6jt1vc1#

重新生成应用。请尝试此代码。

Future<void> getCurrentLocation() async {
      Position position = await Geolocator.getCurrentPosition(
                           desiredAccuracy:LocationAccuracy.high);
      double lat = position.latitude;
      double long = position.longitude;
      print("Latitude: $lat and Longitude: $long");
  }
gzszwxb4

gzszwxb42#

尝试重新启动应用程序!应该从那里开始工作。热重启不会工作时,u添加插件。

txu3uszq

txu3uszq3#

如果您没有根据所需的应用平台对应用进行更改,通常会发生此错误。
即Android、iOS、Web
后藤Usage下的软件包描述中的package
遵循正在构建的平台的实施步骤

ddarikpa

ddarikpa4#

有一些有用的步骤,以遵循您提到的自述包。更新
android\app\build.gradle

android {
  compileSdkVersion 31

  ...
}
lc8prwob

lc8prwob5#

尝试下面的代码希望对您有帮助,请参考geolocatorheregeocodinghere获取 * 纬度 * 和 * 经度 * 以及您的位置名称
为当前位置和init()方法创建位置和一个变量

Position? _currentPosition;
  String? _currentAddress;
  void initState() {
    super.initState();
    getCurrentLocation();
  }

获取纬度和经度的函数

getCurrentLocation() {
    Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best,
            forceAndroidLocationManager: true)
        .then((Position position) {
      setState(() {
        _currentPosition = position;
         print(position.latitude);
         print(position.longitude);
        _getAddressFromLatLng();
      });
    }).catchError((e) {
      print(e);
    });
  }

函数从您的纬度和经度位置

_getAddressFromLatLng() async {
    try {
      List<Placemark> placemarks = await placemarkFromCoordinates(
          _currentPosition!.latitude, _currentPosition!.longitude);

      Placemark place = placemarks[0];

      setState(() {
        _currentAddress = "${place.subLocality}";//here you can used place.country and other things also
        print(_currentAddress);
      });
    } catch (e) {
      print(e);
    }
  }

您的小工具:

Column(
      children: [
        if (_currentPosition != null && _currentAddress != null)
          Text(
            _currentAddress!,
            style: TextStyle(
              fontWeight: FontWeight.bold,
              letterSpacing: .5,
              fontSize: 15,
              color: Colors.black,
            ),
          ),
      ],
    )

application标记上方的android\app\src\main\AndroidManifest.xml文件中添加以下行

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

转到android\app\build.gradle文件并更改以下更改

compileSdkVersion 31
minSdkVersion 21
targetSdkVersion 29

相关问题