【问题标题】:geolocator 6.0.0+1 distanceBetween() not working in fluttergeolocator 6.0.0+1 distanceBetween() 无法正常工作
【发布时间】:2020-12-19 21:44:17
【问题描述】:

我希望每个人都安全我正在使用颤振,我想计算两个位置之间的距离,所以我从 pub.dev here 安装了 geolocator 6.0.0+1,我可以找到用户的位置,但我不能使用名为 ** distanceBetween()** 的内置方法,该方法用于计算两个纬度和经度之间的距离,它向我显示了一个错误,它说 **The method 'distanceBetween' is' t 为“_RegisterState”类型定义。 ** 我使用了文档here中显示的方法

这是我的代码:

import 'dart:html;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:staffattendence05/Usermodel.dart';
import 'package:staffattendence05/add_user.dart';
import 'package:staffattendence05/authenticate.dart';
import 'package:staffattendence05/fauth.dart';
import 'package:staffattendence05/home.dart';
import 'package:staffattendence05/styling.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:staffattendence05/loading.dart';
import 'package:geolocator/geolocator.dart';
import 'Login_ins.dart';

class Register extends StatefulWidget {
  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  @override
  final _formkey = GlobalKey<FormState>();

  String email = '';

  String error = '';

  String password = '';

  bool loading = false;

  final Authservice _auth = Authservice();

  Widget build(BuildContext context) {
    return loading
        ? Loading()
        : Scaffold(
            backgroundColor: Colors.teal,
            appBar: AppBar(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              title: Text(
                "Register your institute",
                style:
                    TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
              ),
              actions: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: FlatButton.icon(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => Loginins()),
                      );
                    },
                    icon: Icon(Icons.perm_identity),
                    label: Text("institute Login"),
                    color: Colors.teal,
                  ),
                )
              ],
              backgroundColor: Colors.green[400],
            ),
            body: Padding(
              padding: const EdgeInsets.all(36.0),
              child: Form(
                key: _formkey,
                child: Column(
                  children: <Widget>[
                    SizedBox(
                      height: 120.0,
                    ),
                    TextFormField(
                      decoration: styling.copyWith(hintText: "Admin email"),
                      validator: (val) {
                        return val.isEmpty
                            ? "please enter an valid email id"
                            : null;
                      },
                      onChanged: (val) {
                        email = val;
                      },
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    TextFormField(
                      obscureText: true,
                      decoration: styling.copyWith(hintText: "Admin password"),
                      validator: (val) => val.length < 6
                          ? "a password should contain atleast 6 characters"
                          : null,
                      onChanged: (val) => password = val,
                    ),
                    SizedBox(
                      height: 25.0,
                    ),
                    RaisedButton(
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(7)),
                      color: Colors.green[400],
                      child: Text(
                        "Register",
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      onPressed: () async {
                        if (_formkey.currentState.validate()) {
                          setState(() {
                            loading = true;
                          });
                          Position Insposition = await getCurrentPosition(
                              desiredAccuracy: LocationAccuracy.best);
                          dynamic result =
                              await _auth.Signupwithemailandpassword(
                                  email, password, Insposition);
                          final String adminname = email;
                          double distanceInMeters = distanceBetween(
                              52.2165157, 6.9437819, 52.3546274, 4.8285838);
                          print("1111111111111$adminname");
                          print(Insposition);
                          if (result == null) {
                            setState(() {
                              loading = false;
                              error = 'Please enter a valid email address!';
                            });
                          } else {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => Home(
                                        adminname: adminname,
                                      )),
                            );
                          }
                        }
                      },
                    ),
                    SizedBox(height: 20.0),
                    Text(
                      error,
                      style: TextStyle(
                          color: Colors.red[400],
                          fontWeight: FontWeight.bold,
                          fontSize: 14),
                    )
                  ],
                ),
              ),
            ),
          );
  }
}

`如果有人指出我犯的错误,那将是可观的。 提前谢谢你。

【问题讨论】:

    标签: android ios flutter dart geolocation


    【解决方案1】:

    这样做,

    double distanceInMeters = await Geolocator().distanceBetween(
                                  52.2165157, 6.9437819, 52.3546274, 4.8285838);
                              
                        
    

    【讨论】:

    • 包中既没有Geolocator类,也没有distanceBetween方法异步。
    • 它可能不是异步函数,但肯定有一个 Geolocator 包,这就是我认为他出错的地方。 import 'package:geolocator/geolocator.dart';..再次检查
    • 有一个地理定位器包。没有Geolocator 类。
    • 是的,你是对的,我刚刚检查了文档,但我认为他应该试一试......除了等待
    • 您的答案对于旧版本的 geolocator 包是正确的。因为他们昨天将版本从 5.x 更新到 6.x,所以您的答案不适合最新版本
    【解决方案2】:

    查看包的 GitHub 页面和源代码,pub.dev 上的文档似乎已过时。正确的用法似乎是:

    double distanceInMeters = GeolocatorPlatform.instance.distanceBetween(
      52.2165157, 
      6.9437819, 
      52.3546274, 
      4.8285838,
    );
    

    【讨论】:

    • GeolocatorPlatform.instanceGeolocator.distanceBetween7.3.0 为我工作
    【解决方案3】:

    [已解决]

    距离

    GeolocatorPlatform.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude);
    

    方位距离

    GeolocatorPlatform.bearingBetween(startLatitude, startLongitude, endLatitude, endLongitude);
    

    在撰写此答案时,官方文档不正确。我尝试了上面的其他两个答案,但如果您尝试这些代码,它对我有用,希望这也能解决您的问题。 [版本:地理定位器6.0.0+1]

    git https://github.com/Baseflow/flutter-geolocator/issues/496#issuecomment-683861804

    【讨论】:

      【解决方案4】:

      以上都不在我的地理定位器包中 - 我已经完成了颤振 pub 升级,在我的 pubspec.yaml 上,参考如下:地理定位器: 解决我的问题的代码是:

       double distance = await geolocator.distanceBetween(double.tryParse(lat1),
          double.tryParse(long1), double.tryParse(lat2), double.tryParse(long2));
      

      【讨论】:

        猜你喜欢
        • 2021-05-28
        • 1970-01-01
        • 2018-06-08
        • 2016-12-01
        • 1970-01-01
        • 2019-07-02
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        相关资源
        最近更新 更多