为什么我没有得到实际的纬度和经度?纬度:“LOCATION”.latitude 的实例和经度:“LOCATION”.longitude 的实例
为什么我在尝试获取实际的纬度和经度时会获取纬度实例和经度实例?这里我使用的是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用大括号:
否则,您只是打印
location
,而不是location.latitude
。You should use curly braces:
Otherwise, you're just printing
location
, notlocation.latitude
.