【问题标题】:Current location using geolocator使用地理定位器的当前位置
【发布时间】:2019-04-11 09:57:28
【问题描述】:

我正在尝试使用颤振的 geolocator sdk 获取当前位置。但我收到此错误。

这是堆栈跟踪

/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-2.1.1/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LastKnownLocationUsingLocationServicesTask.java:4: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
                                 ^
/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-2.1.1/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LocationUpdatesUsingLocationServicesTask.java:5: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
                                 ^
  symbol: class NonNull
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':geolocator:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我正在尝试像这样获取当前位置

    class _MapActivityState extends State<MapActivity> {

      Position currentLocation;

      @override
      void initState() {
        // TODO: implement initState
        super.initState();
       getUserLocation();
      }
        @override
  Widget build(BuildContext context) {
    return Container(
      child: MapboxMap(
        initialCameraPosition: CameraPosition(target: _center, zoom: 15),
        onMapCreated: _onMapCreated,
        myLocationEnabled: true,
      ),
    );
  }
      Future<Position> locateUser() async {
        return Geolocator()
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      }

      getUserLocation() async {
        currentLocation = await locateUser();
        print(currentLocation);
      }
    }

感谢任何帮助

【问题讨论】:

标签: flutter


【解决方案1】:

打开LastKnownLocationUsingLocationServicesTask文件,删除导致此错误的导入语句,再次使用option + enter导入它,我在使用geolocator时看到了这个问题。

同样,打开LocationUpdatesUsingLocationServicesTask 文件并删除导入(有错误)并重新导入。

【讨论】:

  • 这个文件在哪里?
  • 如果您使用的是Android studio,可以使用快捷方式cmd + shift + o并输入文件名。
  • 你需要在Android studio模块中打开这个文件,你目前是在flutter模块中打开它。见how to open it in android module
  • 当您打开 build.gradle 文件时,它会要求您打开此文件以在 Android Studio 中进行编辑,当您这样做时,它将在一个新窗口中打开,您可以在其中使用快捷方式打开 LastKnownLocationUsingLocationServicesTask 和删除导入。
  • 是的。这就是我问你的原因。你昨天也帮了我
猜你喜欢
  • 1970-01-01
  • 2021-03-22
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2013-10-27
  • 2012-10-18
  • 2018-10-17
相关资源
最近更新 更多