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

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.

Related

Flutter: How to get my location high accuracy?

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.

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.

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 possible to get Image GPS location coordinates from the Image?

I'm building a Flutter App where the user can post a photo and it's location. The use can both get a picture from the camera or from the library.
If the user take a picture from camera I can use the GPS location of the device to set the picture's location. I'm trying to get the GSP location of the pictures based on its metadata, but, I didn't found a way to do it yet.
Is possible to do it? How the best way?
Edit: I put "GPS" in the question to make it clear. I'm trying to discover the physical position where the picture was taken. I also added "Flutter" to the question to make it clear that I'm facing the problem of solving this problem in a Flutter App.
I discovered that my previous problem was related to the Camera permissions of the devices. Usually a camera from android access the user location and save the GPS coordinates as Exif tag, but the ios devices usually don't save this by default unless the user allows location permissions to the camera app.
I changed my app to check if the image has GPS coordinates and the user decides if he wants to share his actual location or the image location.
I'm also having to do some math to process the coordinates:
In order to you use the code you have to add the package exif and geoflutterfire to your app.
void _checkGPSData() async {
Map<String, IfdTag> imgTags = await readExifFromBytes( File(image.path).readAsBytesSync() );
if (imgTags.containsKey('GPS GPSLongitude')) {
setState(() {
_imgHasLocation = true;
_imgLocation = exifGPSToGeoFirePoint(imgTags);
});
}
}
GeoFirePoint exifGPSToGeoFirePoint(Map<String, IfdTag> tags) {
final latitudeValue = tags['GPS GPSLatitude'].values.map<double>( (item) => (item.numerator.toDouble() / item.denominator.toDouble()) ).toList();
final latitudeSignal = tags['GPS GPSLatitudeRef'].printable;
final longitudeValue = tags['GPS GPSLongitude'].values.map<double>( (item) => (item.numerator.toDouble() / item.denominator.toDouble()) ).toList();
final longitudeSignal = tags['GPS GPSLongitudeRef'].printable;
double latitude = latitudeValue[0]
+ (latitudeValue[1] / 60)
+ (latitudeValue[2] / 3600);
double longitude = longitudeValue[0]
+ (longitudeValue[1] / 60)
+ (longitudeValue[2] / 3600);
if (latitudeSignal == 'S') latitude = -latitude;
if (longitudeSignal == 'W') longitude = -longitude;
return GeoFirePoint(latitude, longitude);
}
The geolocation metadata is stored as Exif tags provided the image is a JPEG or a RAW image file. In Android this is done through the ExifInterface class.
The tags are TAG_GPS_LATITUDE and TAG_GPS_LONGITUDE:
https://developer.android.com/reference/android/media/ExifInterface.html
Please note the coordinates need to be expressed in the rational format: degrees, minutes and seconds, as dd/1,mm/1,ss/1.
exifLatitudeLongitudePoint(var data) async{
if (data.containsKey('GPS GPSLongitude')) {
final gpsLatitude = data['GPS GPSLatitude'];
final latitudeSignal = data['GPS GPSLatitudeRef']!.printable;
List latitudeRation = gpsLatitude!.values.toList();
List latitudeValue = latitudeRation.map((item) {
return (item.numerator.toDouble() / item.denominator.toDouble());
}).toList();
double latitude = latitudeValue[0] + (latitudeValue[1] / 60) + (latitudeValue[2] / 3600);
if (latitudeSignal == 'S') latitude = -latitude;
latValue(latitude);
final gpsLongitude = data['GPS GPSLongitude'];
final longitudeSignal = data['GPS GPSLongitude']!.printable;
List longitudeRation = gpsLongitude!.values.toList();
List longitudeValue = longitudeRation.map((item) {
return (item.numerator.toDouble() / item.denominator.toDouble());
}).toList();
double longitude =longitudeValue[0] + (longitudeValue[1] / 60) + (longitudeValue[2] / 3600);
if (longitudeSignal == 'W') longitude = -longitude;
lngValue(longitude);
}
}