简体   繁体   English

当前位置与 flutter

[英]Current location with flutter

I am having issue with retrieve the current location.我在检索当前位置时遇到问题。

Step 1: when i press a button it will open the default popup for enable the location service,as i inspected it doesn't enable the location.第 1 步:当我按下按钮时,它将打开默认弹出窗口以启用位置服务,因为我检查过它没有启用位置。

Step 2: If i enable the location manually i am getting the current location,if i do the first step it doesn't work第 2 步:如果我手动启用该位置,我将获得当前位置,如果我执行第一步,则它不起作用

       final Location location = Location();

  LocationData _location;
  String _error;

  Future<void> _getLocation() async {
    setState(() {
      _error = null;
    });
    try {
      final LocationData _locationResult = await location.getLocation();
      setState(() {
        _location = _locationResult;
      });
    } on PlatformException catch (err) {
      setState(() {
        _error = err.code;
      });
    }
  }
 

This is a sample of how to get the location using the location package这是如何使用位置 package 获取位置的示例

Future _getLocation() async {
    Location location = new Location();
    LocationData _pos = await location.getLocation();
    SharedPrefrence().setLatitude(_pos.latitude);
    SharedPrefrence().setLongitude(_pos.longitude);
}

Learn more about setting up the package here此处了解有关设置 package 的更多信息

As i inspected the problem was using by two plugins called正如我检查的那样,问题是由两个名为的插件使用的

flutter_google_places: ^0.2.6

geocoder: ^0.2.1

In the page,The Location location = new Location() in this the Location is used by the two plugin, so the plugins get correlated.在页面中, Location location = new Location()中的Location被两个插件使用,因此插件相互关联。

Because I have two buttons one for get current location and one for search for custom location.因为我有两个按钮,一个用于获取当前位置,一个用于搜索自定义位置。

So what I did was in import I had to add a prefix and use some functions along with prefix name to search location function , now it's working very well.所以我所做的是在导入时我必须添加一个前缀并使用一些函数以及前缀名称来搜索位置 function ,现在它工作得很好。 Now it's asking for permission and show the dialog box enable the location without leaving the app现在它正在请求许可并显示对话框在不离开应用程序的情况下启用位置

Thanks for @Alok and @ByteMe for helping me out感谢@Alok 和@ByteMe 帮助我

import 'package:flutter_google_places/flutter_google_places.dart' as google_place;
import 'package:google_maps_webservice/places.dart' as map_service;
import 'package:location/location.dart';
    
//Current Location Function

      Future _getLocation() async {
        Location location = new Location();
        LocationData _currentPosition = await location.getLocation();
        SharedPrefrence().setLatitude(_currentPosition.latitude.toString());
        SharedPrefrence().setLongitude(_currentPosition.longitude.toString());
       
        Future loginstatus = SharedPrefrence().getLogedIn();
      
          loginstatus.then((data) {
            if (data == true) {
              Navigator.pop(context, true);
              Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (context) => HomeScreen()),
                  ModalRoute.withName("/login"));
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => LoginScreen(),
                ),
              );
            }
          });
        
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM