為什么在嘗試獲取實際緯度和經度時得到緯度和經度實體?這里我使用的是flutter的geolocator包。請有人幫助我,您的努力將不勝感激。
位置.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
uj5u.com熱心網友回復:
您應該使用花括號:
print('Latitude: ${location.latitude} and Longitude: ${location.longitude}');
否則,您只是在列印location,而不是location.latitude.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433647.html
上一篇:無法洗掉除錯顯示檢查橫幅
