为什么我没有得到实际的纬度和经度?纬度:“LOCATION”.latitude 的实例和经度:“LOCATION”.longitude 的实例

发布于 2025-01-10 08:42:10 字数 1585 浏览 0 评论 0原文

为什么我在尝试获取实际的纬度和经度时会获取纬度实例和经度实例?这里我使用的是flutter的geolocator包。请有人帮助我,您的努力将不胜感激。

location.dart

import 'package:geolocator/geolocator.dart';

class LOCATION {
  double latitued = 0.0;
  double longitude = 0.0;
  Future<void> getCurrentLocation() async {
    try {
      Position currentPosition;
      currentPosition = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.low);
      latitued = currentPosition.latitude;
      longitude = currentPosition.longitude;
    } catch (e) {
      print(e);
    }
  }
}

loading_screen.dart

import 'package:flutter/material.dart';
import 'package:clima_v1/services/location.dart';
import 'package:http/http.dart';

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  void getLocation_v1() async {
    LOCATION location = LOCATION();
    await location.getCurrentLocation();
    print('Latitude: $location.latitude and Longitude: $location.longitude');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            //Get the current location
            getLocation_v1();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}

输出:

I/flutter ( 9688): Latitude: Instance of 'LOCATION'.latitude and Longitude: Instance of 'LOCATION'.longitude

Why am I getting instance of latitude and instance of longitude while trying to get the actual latitude and longitude? Here I am using the geolocator package of flutter. Please someone help me your efforts will be appreciated.

location.dart

import 'package:geolocator/geolocator.dart';

class LOCATION {
  double latitued = 0.0;
  double longitude = 0.0;
  Future<void> getCurrentLocation() async {
    try {
      Position currentPosition;
      currentPosition = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.low);
      latitued = currentPosition.latitude;
      longitude = currentPosition.longitude;
    } catch (e) {
      print(e);
    }
  }
}

loading_screen.dart

import 'package:flutter/material.dart';
import 'package:clima_v1/services/location.dart';
import 'package:http/http.dart';

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  void getLocation_v1() async {
    LOCATION location = LOCATION();
    await location.getCurrentLocation();
    print('Latitude: $location.latitude and Longitude: $location.longitude');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            //Get the current location
            getLocation_v1();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}

Output:

I/flutter ( 9688): Latitude: Instance of 'LOCATION'.latitude and Longitude: Instance of 'LOCATION'.longitude

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-01-17 08:42:10

您应该使用大括号:

print('Latitude: ${location.latitude} and Longitude: ${location.longitude}');

否则,您只是打印 location,而不是 location.latitude

You should use curly braces:

print('Latitude: ${location.latitude} and Longitude: ${location.longitude}');

Otherwise, you're just printing location, not location.latitude.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文