首页 > 解决方案 > 如何在 Flutter 中连续跟踪用户位置?

问题描述

我想跟踪用户位置。出于这个原因,我正在使用颤振的位置包。同样,我正在使用 Streams 进行实时跟踪。但是如何在谷歌地图上实时显示用户位置。我在 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;
      },
    );
  }


} 

标签: google-mapsflutterdart

解决方案


推荐阅读