我能够使用geolocator获取当前位置,但是我想在geolocator中不使用lastKnownLocation来缓存和恢复字符串地址:
这是我想做的,如果gps是:
on = get current address and cache the new address
off = restore from cache
代码:
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
child: ListTile(
title: _currentPosition != null? Text(_currentAddress) : Text(**RESTORE_CACHE_HERE**), <-- //I want to restore address here
),
IconButton(
icon: Icon(Icons.refresh),
onPressed: (){
_getCurrentLocation();
},
),
]
)
)
_getCurrentLocation() {
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
_getAddressFromLatLng();
}).catchError((e) {
print(e);
});
}
_getAddressFromLatLng() async {
try {
List<Placemark> p = await geolocator.placemarkFromCoordinates(
_currentPosition.latitude, _currentPosition.longitude);
Placemark place = p[0];
setState(() {
_currentAddress = "${place.country}";
});
} catch (e) {
print(e);
}
}
谢谢您的时间