Geolocator().getCurrentPosition vs StreamSubscription<Position> in flutter - flutter

I am currently using Geolocator().getCurrentPosition to get user current position... but when I am build an app.. I want to calculate the distance between user current location and her/his house (for example)... but since user can move everywhere and stop at some point.. I also want to calculate that new distance... so the route is like this..
Home= A
user first location= B
user second location= C (so user move from B to C)
I need to know the location from A to B and from A to C
...So what I want to ask is..is it enough to use only Geolocator().getCurrentPosition or am I need also use StreamSubscription<Position> to listen for user location changes?
here is my code...
Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
.then((Position position) {
setState(() {
_first = position;
});
_distanceCalculation(_first.latitude, _first.longitude);
}).catchError((error) {
print(error);
});
if I need StreamSubscription<Position> to listen for user location changes how the way to apply StreamSubscription<Position> inside my code

Related

Q: How to play OneShotAnimations in reverse with new rive

I have an app where I have an animated check mark made in rive. It is a one shot animation and I want to play it and have it stay in the end of the animation once it's done and then also be able to reverse it when the user clicks on it again. I tried using a SimpleAnimation but I also wasn't able to achieve what I want but it did stay after the animation was done. I don't, however, know how to play an animation and then reverse it or play another animation.
The check mark looks something like this: https://cln.sh/MwLFNs
They are two separate animations but they are copy pasted but in reverse so I can use either one or both.
What I need is to have the animation play and then when the user clicks on the check, I want the animation to change to another one which would then play. How can I achieve this?
Thanks for the help!
you can use State machines, get the state and decide what to do when you want
// Getting state machine
void _onRiveInit(Artboard artboard) async {
final controller =
StateMachineController.fromArtboard(artboard, 'State Machine 1');
if (controller != null) {
artboard.addController(controller);
_lick = controller.findInput<bool>('Lick') as SMITrigger;
}
}
void _onHandlerStateMachine() => _lick?.value = !_lick!.value;
_onRiveInit is a function that is fired when the Rive file is being loading,
after you get the controller to the state machine to modify his state (This is important the name of the State Machine has to be the same of the animation State machine name)
and to modified the value of the trigger your must get the reference on this way
_lick = controller.findInput('Lick') as SMITrigger;
then you can use
void _onHandlerStateMachine() => _lick?.value = !_lick!.value;
to put or remove the animation on any time, also you can have more than one trigger.
If your need that your animation do something and after a time do something else you can use a debouncer function
_debouncer.run(() {
_onHandlerStateMachine()
});
**Remember update the view if are using state management
when the rive file is load
// Loads a Rive file
Future<void> _loadRiveFile() async {
final bytes = await rootBundle.load(riveFileName);
RiveFile rFile = RiveFile.import(bytes);
final artboard = rFile.mainArtboard;
_onRiveInit(artboard);
globalArtboard = artboard
..addController(
_animationController = SimpleAnimation('idle'),
);
}
Here I have and example of a Rive animation of Teddy, using Flutter with provider
https://github.com/alcampospalacios/flutter-rive-animations

Flutter : Check GPS Location Mode

I have project related using maps and tracking location user , for this i used Geolocator Package. I need to check current location of user , in my flow project first i check last position of user , if last position null then i check current position of user.
Code
Position _currentPosition;
Position get currentPosition => _currentPosition;
Future<void> getCurrentPosition() async {
try {
Position lastPosition = await Geolocator().getLastKnownPosition();
if (lastPosition != null) {
print("Success Get Last Position...");
_currentPosition = lastPosition;
} else {
final currentPosition = await Geolocator().getCurrentPosition();
if (currentPosition != null) {
print("Success Get Your Current Position...");
_currentPosition = currentPosition;
} else {
throw "Can't Get Your Position";
}
}
} catch (e) {
throw e;
}
notifyListeners();
}
Then i calling getCurrentPosition Function on my button OnPressed to get location user like this :
Button GetLocation
void goToMaps() async {
final mapsProvider = context.read<MapsProvider>();
try {
print('Get Location User');
await mapsProvider.getCurrentPosition();
print('Success Get location User');
} catch (e) {
globalF.showToast(message: e.toString(), isError: true, isLongDuration: true);
}
}
First Problem is , When GPS Location Mode is Device Only I can't get LastPosition or Current Position it never print Success Get Location User and still print Get Location User.
Second Problem is , When Gps Location Mode is Battery Saving user location i got is very inaccurate, compared i used mode High Accuracy.
My question is , How can i check if gps location mode != High Accuracy , then i can show warning user to set the gps to High Accuracy.
Similiar Like this question
Thank's.
For the second problem, it happens because in High Accuracy mode android uses both GPS and network location, but in Battery Saving mode GPS module is turned off. If it was native Android, you could check provider of the location if you get location from LocationManager. But provider is not available in Flutter in Position class. You can instead use accuracy. If it is greater than a threshold and not suitable for your application, you can suggest the user to turn on High Accuracy mode.
Another options would be to use other package than Geolocator (however I'm not aware of any package that provides necessary interface) or to modify source code of Geolocatior so that it provides information about which mode is currently active.

How to show current location indication

I'm using the Flutter SDK Version 4.3.1.0.
I get my location updates from the geolocator plugin and want to show the location on the HERE map. It correctly centers the map to the current location but there is no location indicator.
I currently use the following code. What else do I need?
void _showPosition(final GeoCoordinates coordinates) {
_mapView.mapScene.loadSceneForMapScheme(MapScheme.greyDay, (MapError error) {
if (error != null) {
print("Map scene not loaded. MapError: " + error.toString());
return;
}
_mapView.camera.lookAtPointWithDistance(coordinates, 1000);
});
}
I'm using the Flutter Channel beta, v1.17.0-3.4.pre
The HERE SDK for Flutter does not contain a pre-configured location indicator. You can easily create one by adding a circle item onto the map:
For this I would recommend using a MapPolygon that contains a GeoCircle shape. You can then also update the radius of the GeoCircle on the fly to indicate the current horizontal accuracy of the geolocator plugin.

Determine camera change event was programatic

I want to determine whether the camera changed event was initiated from the user or not. (i have to make different actions based on that). So if the user pans a camera with the finger, i have to close sg, but if i moved the camera with the API, i do not.
Currently i cannot decide it was a user event or not, in my OnCameraChangeListener, because the onCameraChange(CameraPosition var1) method does not provide any kind of information about that.
I also tried to save the last marker position which i animated onto programmatically, and check that in the listener method:
map.setOnCameraChangeListener(new GoogelMap.onCameraChangeListener {
public void onCameraChange(CameraPosition position) {
if (!cameraPosition.target.equals(lastClickedMarker)) {
// this is a user event
}
}
I set the lastClickedMarker with the OnMarkerClickListener. I found out i cannot rely on this, because the cameraPosition and lastClickedMarker coordinates will always differ a little, even if really animate to that marker programmatically with animateCamera().
Is there any way to solve this?
You can set a boolean before you change the camera programatically, and check if it is set (and unset) in the onCameraChange method.
Something like this:
// Moving programmatically
cameraMovedProgrammatically = true;
map.animateCamera(cameraUpdate);
And checking it:
public void onCameraChange(CameraPosition position) {
if (cameraMovedProgrammatically) {
// this is not a user event
cameraMovedProgrammatically = false;
} else {
// this is a user event
}
}

Draw the route at runtime

I have an app with Bing Maps and I need to draw the route of the user dinamically.
Now I can only trace the user position with a pushpin. Is there some method to draw route?
UPDATE 1:
I'm using this handler assigned to geolocator.positionChanged:
private void geolocator_DrawRoute(Geolocator sender, PositionChangedEventArgs args)
{
// Need to get back onto UI thread before updating location information
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
() =>
{
//Get the current location
Location location = new Location(args.Position.Coordinate.Point.Position.Latitude,
args.Position.Coordinate.Point.Position.Longitude);
_rotta.Add(location);
if (_rotta.Count > 1)
{
var polyline = new MapPolyline { Locations = _rotta, Color = Colors.Blue, Width = 3 };
_shapeLayer.Shapes.Add(polyline);
}
//Update the position of the GPS pushpin
MapLayer.SetPosition(GpsPushpin, location);
//Update the map view to the current GPS location
MyMap.SetView(location, 18);
}));
}
UPDATE 2:
private void geolocator_DrawRoute(Geolocator sender, PositionChangedEventArgs args)
{
// Need to get back onto UI thread before updating location information
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
() =>
{
//Get the current location
var location = new Location(args.Position.Coordinate.Point.Position.Latitude, args.Position.Coordinate.Point.Position.Longitude);
_rottaLoc.Add(location);
if (_rotta == null)
{
_rotta = new MapPolyline { Locations = _rottaLoc, Color = Colors.Blue, Width = 4 };
}
else
{
_rotta.Locations = _rottaLoc;
}
_shapeLayer.Shapes.Add(_rotta);
//Update the position of the GPS pushpin
MapLayer.SetPosition(GpsPushpin, location);
//Update the map view to the current GPS location
MyMap.SetView(location, 18);
}));
}
To do this the first step would be to get the users position. If you are using JavaScript you can use the Geolocation API to get the users location and to monitor them as they move. Here is some documentation on how to do this: http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/
Once you have this done you can show the users location fairly easily. If you want to draw a polyline to should their path then what you can do is the first time users position is grabbed you can create a polyline object where the first and second coordinate are this first coordinate. You can then add the polyline to the map. The next time the users location is grabbed you can simply add this new location to the polyline's array of locations.