map_helper.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'package:flutter_map/flutter_map.dart';
  2. import '../../utils/atmob_log.dart';
  3. class MapHelper {
  4. MapHelper._();
  5. static const String tag = "FlutterMapHelper";
  6. static MapLocation? _lastLocation;
  7. static late MapPlatform _mapPlatform;
  8. static final List<MapLocationListener> _locationListeners = [];
  9. static Future<void> init() async {
  10. await FlutterMap.init();
  11. _mapPlatform = FlutterMap.getMapPlatform();
  12. initLocationClient();
  13. }
  14. static void initLocationClient() {
  15. _mapPlatform.receiveLocationStream(listener: (location) {
  16. AtmobLog.d(tag, "onLocationChanged: $location");
  17. if (location.errorCode == 0) {
  18. _lastLocation = location;
  19. try {
  20. for (MapLocationListener listener in _locationListeners) {
  21. listener.call(location);
  22. }
  23. } catch (e) {
  24. AtmobLog.e(tag, "onLocationChanged error: $e");
  25. }
  26. }
  27. });
  28. }
  29. static Future<void> startLocation() {
  30. return _mapPlatform.startLocation();
  31. }
  32. static MapLocation? getLastLocation() {
  33. return _lastLocation;
  34. }
  35. static void addLocationListener(MapLocationListener listener) {
  36. _locationListeners.add(listener);
  37. }
  38. static void removeLocationListener(MapLocationListener listener) {
  39. _locationListeners.remove(listener);
  40. }
  41. }