首页 > 解决方案 > 延迟地理定位器

问题描述

我正在尝试跟踪用户的位置,这在第一次执行时效果很好。但是当我在应用程序中执行其他操作然后尝试再次开始跟踪用户的位置时,进程指示器会显示很长时间,然后出现以下消息:

I/art     ( 2803): Background partial concurrent mark sweep GC freed 308336(9MB) AllocSpace objects, 166(23MB) LOS objects, 26% free, 43MB/59MB, paused 2.902ms total 135.647ms 

在此之后它工作得很好。

(这是否意味着我使用了太多内存,因此垃圾收集器花了这么长时间?我必须改变什么?流有问题吗?还是我必须处理一些东西?)

这是我的地理定位器类:

import 'package:geolocator/geolocator.dart';

class GeolocatorService{


Stream<Position> getCurrentLocation(){

return Geolocator.getPositionStream(distanceFilter: 5,);

}

Future<Position> getInitialLocation() async{

  return Geolocator.getCurrentPosition();
}
 
  }

这是地图:

class MapAction extends StatefulWidget {
  MapAction({Key key, this.inputText})
      : super(key: key);
  final String inputText;

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

class MapActionState extends State<MapAction> {

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<Position>(
        stream: GeolocatorService().getCurrentLocation(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {

            return Center(
              child: Container(
                  child: CircularProgressIndicator()),
            );
          } else {

            return Container(
                height: MediaQuery.of(context).size.height,
                width: MediaQuery.of(context).size.width,
                child: GoogleMap(   
                  initialCameraPosition: CameraPosition(
                      target: LatLng(
                          snapshot.data.latitude, snapshot.data.longitude),
                      zoom: 15.0),
                  zoomControlsEnabled: false,
                  //mapType: MapType.satellite,
                  myLocationButtonEnabled: true,
                  myLocationEnabled: true,
                ));
          }
        });
  }
}

提前致谢 :)

标签: flutterdart

解决方案


推荐阅读