首页 > 解决方案 > GPS 关闭消息和持续位置更新颤动

问题描述

我制作了一个应用程序,它使用 geolocator 包来检查 GPS 并获取用户的位置,我使用提供程序包来处理状态。问题是,当 GPS 关闭时,会出现一个红色屏幕,显示纬度为空,我想实现一个屏幕来告诉用户打开 GPS,并相应地更新位置。

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

import 'package:flutter_map/flutter_map.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong/latlong.dart';


class MapState with ChangeNotifier {

  bool locationServiceActive = true;

  MapController _mapController;
  MapController get mapController => _mapController;

  static var _initialPosition;
  var _lastPosition = _initialPosition;
  LatLng get initialPosition => _initialPosition;
  LatLng get lastPosition => _lastPosition;

  MapState(){
    checkGPS();
    _getUserLocation();
  }


  checkGPS() async{
    bool conn = await Geolocator().isLocationServiceEnabled();
    if(conn == false){
      locationServiceActive = false;
    } else {
      locationServiceActive = true;
    }
    notifyListeners();
  }

  void _getUserLocation() async{
    Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print("/////////////////////////////////////////////////////////////////////////////////position");
    print(position);

    _initialPosition = LatLng(position.latitude, position.longitude);
    notifyListeners();
  }
}

更新:-我将我的 _getUserLocation 函数更改为流,如果用户打开或关闭 gps(如果 gps 关闭,它使用最后一个已知位置)效果会更好......但它不打印语句如果位置为空,则在终端中,这很奇怪,只有当有 lat 和 lng 时!!!

这是我所做的修改...

void _getUserLocation() async{
    Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    var geolocator = Geolocator();
    var locationOptions = LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);

    StreamSubscription<Position> positionStream = geolocator.getPositionStream(locationOptions).listen(
      (Position position) {
        print("/////////////////////////////////////////////////////////////////////////// position");
        print(position == null ? 'Unknown' : position.latitude.toString() + ', ' + position.longitude.toString());
      });
      _initialPosition = LatLng(position.latitude, position.longitude);
    notifyListeners();
  }

标签: flutterflutter-provider

解决方案


如果您想通知用户有关 GPS 的信息,您可以使用类似这样的东西

  @override
  void initState() {
    super.initState();
    initPlatformState();
    location.onLocationChanged.listen((result) {
      currentLocation = result;
// your code
      });
    });
  }

  void initPlatformState() async {
    LocationData currentLocation;
    try {
      currentLocation = await location.getLocation();
      error = "";
    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_DENIED')
        error = 'Permission Denied';
      else if (e.code == 'PERMISSION_DENIED_NEVER_ASK')
        error =
            'Permission denied - please ask the user to enable it from the app settings';
      currentLocation = null;
    }
    setState(() {
      currentLocation = currentLocation;
    });
  }

在这里,如果启用,您正在收听位置流。如果不是,则会引发错误。这里使用的包是位置


推荐阅读