bkonyi / FlutterGeofencing

Rough work for Flutter geofencing plugin
BSD 3-Clause "New" or "Revised" License
332 stars 215 forks source link

Failed to add geofence error #80

Open hundredball opened 3 years ago

hundredball commented 3 years ago

I downloaded the FlutterGeofencing code and tried to utilize the geofencing plugin in my project. I created a button for registering geofences. After creating the google map, I pressed the button and got the following error on the Android emulator.

I/flutter (28044): Permission status: PermissionStatus.granted E/GeofencingPlugin(28044): Failed to add geofence: com.google.android.gms.common.api.ApiException: 1004: E/flutter (28044): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(com.google.android.gms.common.api.ApiException: 1004: , null, null, null) E/flutter (28044): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7) E/flutter (28044): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18) E/flutter (28044): E/flutter (28044): #2 GeofencingManager.registerGeofence (package:geofencing/src/geofencing.dart:148:5) E/flutter (28044): E/flutter (28044): #3 _HomePageState._registerGeofences (package:google_maps_geojson/main.dart:398:9) E/flutter (28044): E/flutter (28044):

Since the error code means that the client doesn't have sufficient location permission to perform geofencing operations. I used the permission_handler plugin to assure that location permission is granted before initializing GeofencingManager. Permission is also granted before registering geofences.

Future initPlatformState() async { _status = await Permission.location.request(); print('Location permission status: $_status'); print('Initializing...'); await GeofencingManager.initialize(); print('Initialization done'); }

However, I can successfully add the geofences without any issues when using the physical Android device. Is it possible that there's a conflict between google map and geofencing in the emulator but not in the physical devices?

Below is my flutter version. Any feedbacks are appreciated. Thank you for your time.

Screen Shot 2021-05-27 at 9 56 54 PM
hundredball commented 3 years ago

I realized that I forgot to add background location permission in AndroidManifest.xml.

\<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

After I added it and removed location_permission parts, now it works in both emulator and physical device!