I am trying to show Google Map in my application but when the current location is shown the current location pin is lagging on the map.
So what happens it, I am sitting in one place and when I open the app it shows my current location but after 2-3 seconds the current location pin moves by itself on the map, and as a result my track breaks.
- The goal is to draw a track from my current location and it should not break due to the lagging of my current location.
Can anyone help me for this issue. I am using location package to get the current location.
My Code:
void initState() {
// TODO: implement initState
super.initState();
Future.delayed(new Duration(seconds: 2), () {
initPlatformState();
});
}
initPlatformState() async {
print('TRACK initPlatformState clled');
if (Platform.isIOS) {
if (await Globals.checkLocationPermission(context: context)) {
_locationService = new Location();
await _locationService.changeSettings(
accuracy: LocationAccuracy.HIGH,
interval: 1000,
distanceFilter: 10);
locationSubscription = await _locationService
.onLocationChanged()
.listen((LocationData result) async {
print('result.latitude is ${result.latitude}');
final GoogleMapController controller = await mapController;
print('controller is $controller');
controller.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(result.latitude, result.longitude) == null
? LatLng(0, 0)
: LatLng(result.latitude, result.longitude),
zoom: mapController.cameraPosition.zoom,
)));
if (mounted) {
setState(() {
currentLocation = result;
retriveLocation = true;
});
}
});
} else {
retriveLocation = true;
}
} else {
bool _permission = false;
_locationService = new Location();
await _locationService.changeSettings(
accuracy: LocationAccuracy.HIGH, interval: 1000, distanceFilter: 10);
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
print("Permission: $_permission");
locationSubscription = await _locationService
.onLocationChanged()
.listen((LocationData result) async {
final GoogleMapController controller = await mapController;
controller.moveCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(result.latitude, result.longitude) == null
? LatLng(0, 0)
: LatLng(result.latitude, result.longitude),
zoom: mapController.cameraPosition.zoom,
)));
if (mounted) {
setState(() {
currentLocation = result;
retriveLocation = true;
});
}
});
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
await initPlatformState();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
error = e.message;
}
}
}
setState(() {});
}