使用geolocator的当前位置

时间:2019-04-11 09:57:29

标签: flutter

我正在尝试使用flutter的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);
      }
    }

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

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

类似地,打开LocationUpdatesUsingLocationServicesTask文件并删除导入(有错误)并重新导入。