使用抖动,我尝试使用Geo locator中的StreamSubscription和getPositionStream检索当前位置。 但是我不断收到错误消息:错误:未为类“ Geolocator”定义方法“ getPostionStream”。
这是我的功能:
void getLocationUpdates(){
StreamSubscription<Position> homeTabPostionStream;
homeTabPostionStream = geoLocator.getPositionStream(locationOptions).listen((Position position){
currentPosition = position;
});
在这里我创建了变量:
var geoLocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 4);
根据您的要求,我编写了完整的代码以供参考。
这是完整的代码:
class HomeTab extends StatefulWidget {
@override
_HomeTabState createState() => _HomeTabState();
}
class _HomeTabState extends State<HomeTab> {
GoogleMapController mapController;
Completer<GoogleMapController> _controller = Completer();
Position currentPosition;
DatabaseReference tripRequestRef;
var geoLocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 4);
void getCurrentPosition() async{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation);
currentPosition = position;
LatLng pos = LatLng(position.latitude, position.longitude);
mapController.animateCamera(CameraUpdate.newLatLng(pos));
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
GoogleMap(
padding: EdgeInsets.only(top: 135),
myLocationEnabled: true,
myLocationButtonEnabled: true,
mapType: MapType.normal,
initialCameraPosition: googlePlex,
onMapCreated: (GoogleMapController controller){
_controller.complete(controller);
mapController = controller;
getCurrentPosition();
},
),
Container(
height: 135,
width: double.infinity,
color: BrandColors.colorPrimary,
),
Positioned(
top: 60,
left: 0,
right: 0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AvailabilityButton(
title: 'GO ONLINE',
color: BrandColors.colorOrange,
onPressed: (){
GoOnline();
},
),
],
),
),
],
);
}
void GoOnline(){
Geofire.initialize('driversAvailable');
Geofire.setLocation(currentFirebaseUser.uid, currentPosition.latitude, currentPosition.longitude);
tripRequestRef = FirebaseDatabase.instance.reference().child('drivers/${currentFirebaseUser.uid}/newtrip');
tripRequestRef.set('waiting');
tripRequestRef.onValue.listen((event) {
});
}
void getLocationUpdates(){
StreamSubscription<Position> homeTabPostionStream;
homeTabPostionStream = geoLocator.getPositionStream(locationOptions).listen((Position position){
currentPosition = position;
});
答案 0 :(得分:3)
无需实例化Geolocator
。 getPositionStream()
是一个静态方法,称为Geolocator.getPositionStream()
。传递给该方法的args都是可选参数。不需要locationOptions。
已更新
void getLocationUpdates(){
StreamSubscription<Position> homeTabPostionStream;
homeTabPostionStream = Geolocator.getPositionStream(
desiredAccuracy: LocationAccuracy.bestForNavigation,
distanceFilter: 4).listen((event) {
...
});
}
您可以删除
var geoLocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 4);
答案 1 :(得分:0)
您的方法似乎没有一个字母(i):getPostionStream。它必须像:
void getLocationUpdates(){
StreamSubscription<Position> homeTabPostionStream;
homeTabPostionStream = geoLocator.getPositionStream().listen((Position position){
currentPosition = position;
});