如何在Flutter中连续跟踪用户位置?

时间:2020-04-06 21:54:26

标签: google-maps flutter dart

我想跟踪用户位置。因此,我正在使用flutter的位置包。同样,我正在使用Streams进行实时跟踪。但是如何在Google地图上实时显示用户位置。我对在Google地图上显示实时位置有一些疑问。

**这是我的定位服务课程**

class LocationService {

 UserLocation _currentLocationofUser;

 Location location = Location();

 //Sürekli güncelle
 StreamController<UserLocation> _locationController =
     StreamController<UserLocation>.broadcast();

 LocationService() {
   location.requestPermission().then((granted) {
     if (granted != null) {
       location.onLocationChanged.listen((locationData) {
         if (locationData != null) {
           _locationController.add(UserLocation(
               latitude: locationData.latitude,
               longitude: locationData.longitude));
         }
       });
     }
   });
 }

 Stream<UserLocation> get locationStream => _locationController.stream;

**这是我的主要课程,用于显示用户**的实时位置

  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  GoogleMapController _mapController;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var userLocation = Provider.of<UserLocation>(context);

    return GoogleMap(
      mapType: MapType.normal,
      initialCameraPosition: CameraPosition(
          zoom: 12,
          target: LatLng(userLocation.latitude, userLocation.longitude)),
      onMapCreated: (GoogleMapController controller) {
        _mapController = controller;
      },
    );
  }


} 

0 个答案:

没有答案
相关问题