add cache for latitude and use in searchdelagate
This commit is contained in:
parent
23aec689f1
commit
627eb778ad
@ -367,7 +367,11 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
suffixIcon: IconButton(
|
suffixIcon: IconButton(
|
||||||
icon: const Icon(Icons.clear),
|
icon: const Icon(Icons.clear),
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
|
SharedPreferences prefs =
|
||||||
|
await SharedPreferences.getInstance();
|
||||||
|
prefs.remove("city_lat");
|
||||||
|
prefs.remove("city_long");
|
||||||
setState(() {
|
setState(() {
|
||||||
Datepicker.text = '';
|
Datepicker.text = '';
|
||||||
});
|
});
|
||||||
@ -464,7 +468,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
showSearch(
|
showSearch(
|
||||||
context: context,
|
context: context,
|
||||||
delegate: SearchDelegateExample(geoQuery: inputGeo.text),
|
delegate: SearchDelegateExample(),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
@ -11,11 +11,7 @@ import 'dart:math';
|
|||||||
import '../variable/globals.dart' as globals;
|
import '../variable/globals.dart' as globals;
|
||||||
|
|
||||||
class SearchDelegateExample extends SearchDelegate {
|
class SearchDelegateExample extends SearchDelegate {
|
||||||
final String geoQuery;
|
SearchDelegateExample();
|
||||||
|
|
||||||
SearchDelegateExample({
|
|
||||||
required this.geoQuery,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Widget> buildActions(BuildContext context) {
|
List<Widget> buildActions(BuildContext context) {
|
||||||
@ -43,7 +39,7 @@ class SearchDelegateExample extends SearchDelegate {
|
|||||||
Widget buildResults(BuildContext context) {
|
Widget buildResults(BuildContext context) {
|
||||||
// Perform the search and return the results
|
// Perform the search and return the results
|
||||||
return FutureBuilder<List<Events>>(
|
return FutureBuilder<List<Events>>(
|
||||||
future: searchPosts(query, geoQuery),
|
future: searchPosts(query),
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
@ -79,46 +75,29 @@ class SearchDelegateExample extends SearchDelegate {
|
|||||||
return Container(); // Implement suggestions if needed
|
return Container(); // Implement suggestions if needed
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Events>> searchPosts(String query, String geoQuery) async {
|
Future<List<Events>> searchPosts(String query) async {
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
var accessToken = prefs.getString("access_token") ?? "";
|
var accessToken = prefs.getString("access_token") ?? "";
|
||||||
|
var latitude = prefs.getDouble("city_lat") ?? 0.0;
|
||||||
|
var longitude = prefs.getDouble("city_long") ?? 0.0;
|
||||||
final List<Events> body = [];
|
final List<Events> body = [];
|
||||||
if (accessToken.isNotEmpty) {
|
if (accessToken.isNotEmpty) {
|
||||||
DateTime currentDate = DateTime.now();
|
DateTime currentDate = DateTime.now();
|
||||||
var url = Uri.parse(
|
var url = Uri.parse(
|
||||||
"${globals.api}/events/search?item=${query}¤t_dateime=${currentDate.toString()}");
|
"${globals.api}/events/search?item=${query}¤t_dateime=${currentDate.toString()}");
|
||||||
if (geoQuery.isNotEmpty) {
|
if ((latitude != 0.0) && (longitude != 0.0)) {
|
||||||
await dotenv.load(
|
|
||||||
fileName: ".env"); // Load your .env for the Mapbox access token
|
|
||||||
final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? '';
|
|
||||||
final geocodeUrl = Uri.parse(
|
|
||||||
'https://api.mapbox.com/geocoding/v5/mapbox.places/$geoQuery.json?access_token=$mapboxAccessToken');
|
|
||||||
final geocodeResponse = await http.get(geocodeUrl);
|
|
||||||
if (geocodeResponse.statusCode == 200) {
|
|
||||||
final geocodeData = json.decode(geocodeResponse.body);
|
|
||||||
if (geocodeData['features'].isNotEmpty) {
|
|
||||||
final coordinates =
|
|
||||||
geocodeData['features'][0]['geometry']['coordinates'];
|
|
||||||
final longitude = coordinates[0]; // Longitude
|
|
||||||
final latitude = coordinates[1]; // Latitude
|
|
||||||
|
|
||||||
// Now use the latitude and longitude to get events within a 50km radius
|
// Now use the latitude and longitude to get events within a 50km radius
|
||||||
double radiusInKm = 50;
|
double radiusInKm = 50;
|
||||||
double latDistance = radiusInKm / 111.0;
|
double latDistance = radiusInKm / 111.0;
|
||||||
double lonDistance =
|
double lonDistance = radiusInKm / (111.0 * cos(latitude * pi / 180));
|
||||||
radiusInKm / (111.0 * cos(latitude * pi / 180));
|
|
||||||
|
|
||||||
double minLat = latitude - latDistance;
|
double minLat = latitude - latDistance;
|
||||||
double maxLat = latitude + latDistance;
|
double maxLat = latitude + latDistance;
|
||||||
double minLon = longitude - lonDistance;
|
double minLon = longitude - lonDistance;
|
||||||
double maxLon = longitude + lonDistance;
|
double maxLon = longitude + lonDistance;
|
||||||
|
|
||||||
// Construct the search URL with the item query and latitude/longitude bounds
|
// Construct the search URL with the item query and latitude/longitude bounds
|
||||||
url = Uri.parse(
|
url = Uri.parse(
|
||||||
"${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}¤t_dateime=${currentDate.toString()}");
|
"${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}¤t_dateime=${currentDate.toString()}");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
final response = await http.get(url, headers: {
|
final response = await http.get(url, headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user