如何在 flutter 中使用 geoLocator 包在我的控制台上打印当前位置?
How can I print the current location on my console using geoLocator package in flutter?
这是我在 loading_screen.dart 文件中的代码。我已经明确提到打印我的位置,甚至在 onpressed 中调用该方法。仍然没有任何反应。
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void getLocation() async{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}
您的实施中缺少几个步骤。
首先您需要检查权限,如果允许,则获取位置,否则请求权限。
您还需要在清单中为 android 添加权限,并在 info.plist 中为 ios
添加权限
示例代码:-
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Test(),
);
}
}
class Test extends StatefulWidget{
@override
_Test createState() => _Test();
}
class _Test extends State<Test>{
void getLocation() async{
LocationPermission permission = await Geolocator.checkPermission();
if(permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
printLocation();
}else{
requestPermission();
}
}
requestPermission() async{
LocationPermission permission = await Geolocator.requestPermission();
if(permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
printLocation();
}else{
requestPermission();
}
}
printLocation() async{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low, timeLimit: Duration(seconds: 10));
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}
这是我在 loading_screen.dart 文件中的代码。我已经明确提到打印我的位置,甚至在 onpressed 中调用该方法。仍然没有任何反应。
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
void getLocation() async{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}
您的实施中缺少几个步骤。
首先您需要检查权限,如果允许,则获取位置,否则请求权限。
您还需要在清单中为 android 添加权限,并在 info.plist 中为 ios
添加权限示例代码:-
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Test(),
);
}
}
class Test extends StatefulWidget{
@override
_Test createState() => _Test();
}
class _Test extends State<Test>{
void getLocation() async{
LocationPermission permission = await Geolocator.checkPermission();
if(permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
printLocation();
}else{
requestPermission();
}
}
requestPermission() async{
LocationPermission permission = await Geolocator.requestPermission();
if(permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
printLocation();
}else{
requestPermission();
}
}
printLocation() async{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low, timeLimit: Duration(seconds: 10));
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}