I have Users with 'Location' (geopoint). Then I have 'Locations" Collection with each document holding a 'Map' (geopoint), 'State' (string and 'Town' (string).
I'm looking to grab the users Location, then determine which Town they're closest to.
The action I was trying to refine just wouldn't work. I also had to make 2 functions, to take the user geopoint and turn lat and lng into seperate doubles to pass into my action. Cannot figure this out. I've included my action and functions below.
Any advice/help would be appreciated.
What have you tried so far?
user Location to Lat Double: (was the same for Lng double)
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/auth/firebase_auth/auth_util.dart';
double? latitudeFromLocation(LatLng? location) {
/// MODIFY CODE ONLY BELOW THIS LINE
// return latitude as double
if (location != null) {
return location.latitude;
} else {
return null;
}
/// MODIFY CODE ONLY ABOVE THIS LINE
}
Then the action:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
const R = 6371; // Radius of the Earth in km
final dLat = (lat2 - lat1) * pi / 180;
final dLon = (lon2 - lon1) * pi / 180;
final a = sin(dLat / 2) * sin(dLon / 2) +
cos(lat1 * pi / 180) *
cos(lat2 * pi / 180) *
sin(dLon / 2) *
sin(dLon / 2);
final c = 2 * atan2(sqrt(a), sqrt(1 - a));
return R * c; // Distance in km
}
Future<String> findNearestTown(double userLat, double userLng) async {
final locationsSnapshot =
await FirebaseFirestore.instance.collection('Locations').get();
final locations = locationsSnapshot.docs.map((doc) => doc.data()).toList();
double minDistance = double.infinity;
String nearestTown = '';
for (var location in locations) {
final townLatLng = location['Map'] as GeoPoint; // Assuming 'Map' is the field name
final distance = calculateDistance(
userLat,
userLng,
townLatLng.latitude,
townLatLng.longitude,
);
if (distance < minDistance) {
minDistance = distance;
nearestTown = location['Town'] as String;
}
}
return nearestTown;
}
// Custom action main function
Future<String> customAction({
required double userLat,
required double userLng,
}) async {
return await findNearestTown(userLat, userLng);
}
Did you check FlutterFlow's Documentation for this topic?