Flutter: How to get my location high accuracy? - flutter

Future<Position> mylocation() async{
Position position = await Geolocator().getCurrentPosition(desiredAccuracy:LocationAccuracy.bestForNavigation);
return position;
}
I want to get my location. but sometime mylocation get wrong position. How to get my location high accuracy?

Try this
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best);

if you want to get the location high accuracy you can use
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
Make sure to adjust the accuracy and read the docs to fit your use case.

Related

Why do my functions under _onCameraMove run so poorly?

If I pass latlnglastPosition to my widget, it's fine. I can see latitude and longitude and the value changes with zero lag as my camera position changes. However, using anything more than that causes crazy lag. It's practically unusable. Does anybody know what's causing this or if there's a better way of doing this? I'm trying to display latitude, longitude, altitude, distance from center versus current position in meters and miles. Here's the code:
void _onCameraMove(CameraPosition cameraPosition) async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
lat = position.latitude;
lng = position.longitude;
alt = position.altitude;
_distancebetween = await Geolocator().distanceBetween(
_lastMapPosition.latitude, _lastMapPosition.longitude, lat, lng);
distancebetweenFinal = _distancebetween.toStringAsFixed(2);
distancebetweenFinalMiles = _distancebetween * (0.000621371);
_lastMapPosition = cameraPosition.target;
var latlnglastPosition = _lastMapPosition;
_currentZoom = cameraPosition.zoom;
setState(() {
latlnglastPosition;
distancebetweenFinal;
distancebetweenFinalMiles;
_distancebetween;
_lastMapPosition;
});
}
This is using Flutter Google Maps.
Edit: I removed the conversion from meters to miles. It didn't help. I don't think the altitude is having any impact. It's just going from current camera view position to my current position that's delaying everything. Is there perhaps a way to increase the frequency i receive location info? maybe that's hanging things up.... ?
Edit: or maybe I can use get last known position when a new location is not available? Not sure if that's the best way to handle this
Edit: I set lat = 34.0; lng = 34.0; and it's super fast now. However, it doesn't solve my problem. I need lat and lng to reflect my current position, and it seems i'm either restricted in pulling my current position or it's too costly to pull it as frequently as I need to. I don't know the best way to handle this... Any ideas are appreciated.
I got it (I think):
I replaced
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
with:
Position position = await Geolocator().getLastKnownPosition();
I think position.latitude and position.longitude were too costly before. So, I'm getting the last known position. If anyone knows a better way, please let me know.

Get map center point on scrolling Google maps flutter

I'm doing an app in my app. I'm using google maps flutter and I'm having an issue regarding scrolling. When I scroll the map I want to get the center points (long, lat) of the map that is if I scroll the map and stop at certain place it would grab the location points of center.
Can anyone suggest how to solve this issue? It can be very helpful Thank you in advance..
I would get the bounds of the screen, and then find the center of those bounds. If you have a GoogleMapController for the GoogleMap, just use a function like this:
getCenter() async {
LatLngBounds bounds = await mapsController.getVisibleRegion();
LatLng center = LatLng(
(bounds.northeast.latitude + bounds.southwest.latitude) / 2,
(bounds.northeast.longitude + bounds.southwest.longitude) / 2,
);
return center;
}
In the latest version of google maps flutter we can get the coordinates of location by passing in the screen coordinatinates,
// some code here
Size screen = MediaQuery.of(context).size;
// some code here
final coords = await mapController.getLatLng(ScreenCoordinate( x: screen.width/2, y:
screen.width/2));

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.

Geolocator().getCurrentPosition vs StreamSubscription<Position> in 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

Is there a way to match users based on their location?

Essentially I am hoping to have the app register the location of user A who is actively using the app. Then register the location of user B who is also active and then match them if they are within a 20 mile radius.
you can achieve this using geolocator. Please follow all the instruction mention in geolocator.
Then import geolocator.
import 'package:geolocator/geolocator.dart';
Future<double> calculateDistance(double startLatitude,double startLongitude) async{
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);//get user's current location
double endLatitude = position.latitude;
double endLongitude = position.longitude;
double distanceInMeters = await Geolocator().distanceBetween(startLatitude,startLongitude, endLatitude, endLongitude);
return distanceInMeters * 0.000621371192;//convert meters into miles.
}
You need to check if user granted the location permission.