Return 当前位置的文本 Flutter
Return Text with current position Flutter
我想 return 一些字符串或文本 :
- 城市名称
- 邮政编码
- 国家
所以这是我的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
class Maps extends StatefulWidget {
static Future<void> show(
BuildContext context,
) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Maps(),
fullscreenDialog: true,
),
);
}
@override
_MapsState createState() => _MapsState();
}
Future<Position> locateUser() async {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
class _MapsState extends State<Maps> {
Geolocator geolocator = Geolocator();
Position _currentPosition;
String _location;
String _addressLine;
bool done = false;
@override
Widget build(BuildContext context) {
_getCurrentLocation();
_getLocation(_currentPosition);
return Scaffold(
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width * 0.6,
child: Center(
child: Column(
children: [
done == false
? Text("Need to get the position")
: Text("val 1: $_location, val 2: $_addressLine"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
),
),
);
}
Future<void> _getLocation(Position position) async {
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
List<Address> addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
Address first = addresses.first;
_location = "${first.featureName}";
_addressLine = " ${first.addressLine}";
done = true;
}
void _getCurrentLocation() {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
}
}
问题是它总是 returned“需要获得位置”而不是真实地址。
我知道这段代码不是最好的,但它是经过数小时研究得出的结果,我没有找到关于使用地理函数的一些明确解释。
我只是对您的代码进行了一些更改,因为我不想破坏您的代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
class Maps extends StatefulWidget {
static Future<void> show(
BuildContext context,
) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Maps(),
fullscreenDialog: true,
),
);
}
@override
_MapsState createState() => _MapsState();
}
Future<Position> locateUser() async {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
class _MapsState extends State<Maps> {
Geolocator geolocator = Geolocator();
Position _currentPosition;
String _location;
String _addressLine;
bool done = false;
@override
void initState() {
_getCurrentLocation();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width * 0.6,
child: Center(
child: Column(
children: [
done == false
? Text("Need to get the position")
: Text("val 1: $_location, val 2: $_addressLine"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
),
),
);
}
Future<void> _getLocation(Position position) async {
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
List<Address> addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
Address first = addresses.first;
_location = "${first.featureName}";
_addressLine = " ${first.addressLine}";
setState(() {
done = true;
});
}
void _getCurrentLocation() {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
_getLocation(position);
}).catchError((e) {
print(e);
});
}
}
我想 return 一些字符串或文本 :
- 城市名称
- 邮政编码
- 国家
所以这是我的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
class Maps extends StatefulWidget {
static Future<void> show(
BuildContext context,
) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Maps(),
fullscreenDialog: true,
),
);
}
@override
_MapsState createState() => _MapsState();
}
Future<Position> locateUser() async {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
class _MapsState extends State<Maps> {
Geolocator geolocator = Geolocator();
Position _currentPosition;
String _location;
String _addressLine;
bool done = false;
@override
Widget build(BuildContext context) {
_getCurrentLocation();
_getLocation(_currentPosition);
return Scaffold(
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width * 0.6,
child: Center(
child: Column(
children: [
done == false
? Text("Need to get the position")
: Text("val 1: $_location, val 2: $_addressLine"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
),
),
);
}
Future<void> _getLocation(Position position) async {
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
List<Address> addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
Address first = addresses.first;
_location = "${first.featureName}";
_addressLine = " ${first.addressLine}";
done = true;
}
void _getCurrentLocation() {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
}
}
问题是它总是 returned“需要获得位置”而不是真实地址。 我知道这段代码不是最好的,但它是经过数小时研究得出的结果,我没有找到关于使用地理函数的一些明确解释。
我只是对您的代码进行了一些更改,因为我不想破坏您的代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
class Maps extends StatefulWidget {
static Future<void> show(
BuildContext context,
) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Maps(),
fullscreenDialog: true,
),
);
}
@override
_MapsState createState() => _MapsState();
}
Future<Position> locateUser() async {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
class _MapsState extends State<Maps> {
Geolocator geolocator = Geolocator();
Position _currentPosition;
String _location;
String _addressLine;
bool done = false;
@override
void initState() {
_getCurrentLocation();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width * 0.6,
child: Center(
child: Column(
children: [
done == false
? Text("Need to get the position")
: Text("val 1: $_location, val 2: $_addressLine"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
),
),
);
}
Future<void> _getLocation(Position position) async {
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
List<Address> addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
Address first = addresses.first;
_location = "${first.featureName}";
_addressLine = " ${first.addressLine}";
setState(() {
done = true;
});
}
void _getCurrentLocation() {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
_getLocation(position);
}).catchError((e) {
print(e);
});
}
}