来自地理定位器的共享首选项保存位置地址
Shared preference save location address from geolocator
我能够使用地理定位器获取当前位置,但我想在不使用地理定位器中的 lastKnownLocation 的情况下缓存和恢复字符串地址。我正在使用共享首选项,但无法使其正常工作。我在我的其他代码中多次使用共享首选项,但使用地理定位器有点复杂。我是 flutter/dart
的超级新手
代码:
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
Position _currentPosition;
String _currentAddress;
String _locationCache;
String key = "location_cache";
@override
void initState() {
super.initState();
_getCurrentLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("current location = " + _currentAddress),
Text("last location = " + __locationCache) // HERE GET STORED DATA ISNT WORKING
],
),
),
);
}
_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}";
});
saveAddress();
} catch (e) {
print(e);
}
}
Future<bool> saveAddress() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(key, _currentAddress);
}
Future<String> retrieveAddress() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
return prefs.getString(key) ?? "";
}
loadAddress() {
retrieveAddress().then((value) {
setState(() {
_locationCache = value;
});
});
}
}
这是没有 _locationCache 的工作代码:
感谢您的宝贵时间
如果我对您的理解正确,您想要完成的是存储您捕获的最后一个地址,并在您没有启用 gps 时检索它。
为此,您可以使用 SharedPreferences or SQLite,只需查看有关如何使用它们的文档即可。
找到解决方案。只需将 loadAddress()
函数替换为
void save() {
String address = _currentAddress;
saveAddress(address);
}
void _updateName(String address) {
setState(() {
this.locationCache = address;
});
}
然后把retrieveAddress().then(updateName)
放在initState()
里面
我能够使用地理定位器获取当前位置,但我想在不使用地理定位器中的 lastKnownLocation 的情况下缓存和恢复字符串地址。我正在使用共享首选项,但无法使其正常工作。我在我的其他代码中多次使用共享首选项,但使用地理定位器有点复杂。我是 flutter/dart
的超级新手代码:
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
Position _currentPosition;
String _currentAddress;
String _locationCache;
String key = "location_cache";
@override
void initState() {
super.initState();
_getCurrentLocation();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("current location = " + _currentAddress),
Text("last location = " + __locationCache) // HERE GET STORED DATA ISNT WORKING
],
),
),
);
}
_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}";
});
saveAddress();
} catch (e) {
print(e);
}
}
Future<bool> saveAddress() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(key, _currentAddress);
}
Future<String> retrieveAddress() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
return prefs.getString(key) ?? "";
}
loadAddress() {
retrieveAddress().then((value) {
setState(() {
_locationCache = value;
});
});
}
}
这是没有 _locationCache 的工作代码:
感谢您的宝贵时间
如果我对您的理解正确,您想要完成的是存储您捕获的最后一个地址,并在您没有启用 gps 时检索它。
为此,您可以使用 SharedPreferences or SQLite,只需查看有关如何使用它们的文档即可。
找到解决方案。只需将 loadAddress()
函数替换为
void save() {
String address = _currentAddress;
saveAddress(address);
}
void _updateName(String address) {
setState(() {
this.locationCache = address;
});
}
然后把retrieveAddress().then(updateName)
放在initState()