为什么我没有得到实际的纬度和经度?纬度:'LOCATION'.latitude 的实例和经度:'LOCATION'.longitude 的实例
Why i am not getting the actual latitude and longitude? Latitude: Instance of 'LOCATION'.latitude and Longitude: Instance of '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
你应该使用花括号:
print('Latitude: ${location.latitude} and Longitude: ${location.longitude}');
否则,您只是打印 location
,而不是 location.latitude
。
为什么我在尝试获取实际纬度和经度时得到了纬度实例和经度实例?这里我使用的是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
你应该使用花括号:
print('Latitude: ${location.latitude} and Longitude: ${location.longitude}');
否则,您只是打印 location
,而不是 location.latitude
。