【问题标题】:I want to resolve geolocation errors我想解决地理定位错误
【发布时间】:2022-01-05 04:22:37
【问题描述】:

我正在尝试使用GeoLocator 获取我当前位置的纬度和经度。 我阅读了文档。 我是 Flutter 初学者 我在 info.split 中添加了键、字符串,但错误并没有消失。 我搜索过,但我遇到了麻烦,因为我找不到解决方案。

错误信息

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: User denied permissions to access the device's location.
#0      MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:127:7)
<asynchronous suspension>
#1      _MyHomePageState.getLocation (package:applicationname/main.dart:37:25)
<asynchronous suspension>

我添加了 main.dart 文件。 我将在这段代码中获取位置数据。

main.dart

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _location = "no data";
 
  getLocation() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    setState(() {
      _location = position.toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_location),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              '_location',
            ),
            Text(
              '_location',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: getLocation, child: Icon(Icons.location_on)),
    );
  }
}

【问题讨论】:

  • 你在android清单文件中添加了位置权限吗?
  • 是的。我在问题文本中添加了 AndroidManifest.xml 的代码。
  • 没关系。错误在于无法获取位置。如果可能,添加 main.dart 文件。
  • 如果您想获取当前位置,请参考我的回答 here

标签: flutter dart geolocation


【解决方案1】:

试试这个,

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _location = "no data";
 
  getLocation() async {
    final hasPermission = await _handlePermission();

    if (!hasPermission) {
      return;
    }
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    setState(() {
      _location = position.toString();
    });
  }
  
    Future<bool> _handlePermission() async {
    bool serviceEnabled;
    LocationPermission permission;

    // Test if location services are enabled.
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // do stuff
      return false;
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        // do stuff
        return false;
      }
    }

    if (permission == LocationPermission.deniedForever) {
      // Permissions are denied forever, handle appropriately.
      // do stuff

      return false;
    }
    return true;
  }
  
  @override
  void initState() {
  getLocation();
  super.initState();
    }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_location),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              '_location',
            ),
            Text(
              '_location',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: getLocation, child: Icon(Icons.location_on)),
    );
  }
}

【讨论】:

  • 成功感谢您。请告诉我是什么原因。
  • 位置许可的原因。
【解决方案2】:

使用此代码,我们不能一次又一次地强制发送位置许可请求。使用此代码

 static getLocation() async{
LocationPermission permission = await Geolocator.checkPermission();

if(permission == LocationPermission.denied){
  await Geolocator.requestPermission();

  return;
}else if(permission == LocationPermission.deniedForever){
  await Geolocator.openLocationSettings();

  return;
}else{
  final position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
   print("latitude = ${position.latitude}");
   print("longitude= ${position.longitude}");
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多