fix: reverse geocode stored coordinates to place names
When lat,lng coordinates are stored in SharedPreferences from a previous session, reverse geocode them to a human-readable location name (e.g. "Whitefield, Bengaluru") instead of showing raw numbers like "10.57376,76.01188". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ import 'contribute_screen.dart';
|
|||||||
import 'learn_more_screen.dart';
|
import 'learn_more_screen.dart';
|
||||||
import 'search_screen.dart';
|
import 'search_screen.dart';
|
||||||
import '../core/app_decoration.dart';
|
import '../core/app_decoration.dart';
|
||||||
|
import 'package:geocoding/geocoding.dart';
|
||||||
import 'package:flutter_svg/flutter_svg.dart';
|
import 'package:flutter_svg/flutter_svg.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import '../features/gamification/providers/gamification_provider.dart';
|
import '../features/gamification/providers/gamification_provider.dart';
|
||||||
@@ -83,9 +84,16 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
|
|||||||
_username = prefs.getString('display_name') ?? prefs.getString('username') ?? '';
|
_username = prefs.getString('display_name') ?? prefs.getString('username') ?? '';
|
||||||
final storedLocation = prefs.getString('location') ?? 'Whitefield, Bengaluru';
|
final storedLocation = prefs.getString('location') ?? 'Whitefield, Bengaluru';
|
||||||
// Fix legacy lat,lng strings saved before the reverse-geocoding fix
|
// Fix legacy lat,lng strings saved before the reverse-geocoding fix
|
||||||
if (RegExp(r'^-?\d+\.\d+,-?\d+\.\d+$').hasMatch(storedLocation)) {
|
final coordMatch = RegExp(r'^(-?\d+\.?\d*),\s*(-?\d+\.?\d*)$').firstMatch(storedLocation);
|
||||||
|
if (coordMatch != null) {
|
||||||
_location = 'Current Location';
|
_location = 'Current Location';
|
||||||
prefs.setString('location', _location);
|
setState(() {});
|
||||||
|
// Reverse geocode in background to get actual place name
|
||||||
|
_reverseGeocodeAndSave(
|
||||||
|
double.parse(coordMatch.group(1)!),
|
||||||
|
double.parse(coordMatch.group(2)!),
|
||||||
|
prefs,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
_location = storedLocation;
|
_location = storedLocation;
|
||||||
}
|
}
|
||||||
@@ -116,6 +124,24 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _reverseGeocodeAndSave(double lat, double lng, SharedPreferences prefs) async {
|
||||||
|
try {
|
||||||
|
final placemarks = await placemarkFromCoordinates(lat, lng);
|
||||||
|
if (placemarks.isNotEmpty) {
|
||||||
|
final p = placemarks.first;
|
||||||
|
final parts = <String>[];
|
||||||
|
if ((p.subLocality ?? '').isNotEmpty) parts.add(p.subLocality!);
|
||||||
|
if ((p.locality ?? '').isNotEmpty) parts.add(p.locality!);
|
||||||
|
if ((p.subAdministrativeArea ?? '').isNotEmpty) parts.add(p.subAdministrativeArea!);
|
||||||
|
final label = parts.isNotEmpty ? parts.join(', ') : 'Current Location';
|
||||||
|
await prefs.setString('location', label);
|
||||||
|
if (mounted) setState(() => _location = label);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (_) {}
|
||||||
|
await prefs.setString('location', 'Current Location');
|
||||||
|
}
|
||||||
|
|
||||||
Future<List<EventTypeModel>> _events_service_getEventTypesSafe() async {
|
Future<List<EventTypeModel>> _events_service_getEventTypesSafe() async {
|
||||||
try {
|
try {
|
||||||
return await _eventsService.getEventTypes();
|
return await _eventsService.getEventTypes();
|
||||||
|
|||||||
Reference in New Issue
Block a user