hotfix: map sketch

This commit is contained in:
imlildud
2026-05-23 01:46:01 -06:00
parent 88532b43e7
commit c0b88fea86
3 changed files with 92 additions and 129 deletions

View File

@@ -1,44 +1,41 @@
// src/services/geolocation_service.dart // src/services/geolocation_service.dart
import 'package:geolocator/geolocator.dart'; import 'package:geolocator/geolocator.dart';
import 'dart:async';
import 'package:universal_platform/universal_platform.dart'; class SimplePosition {
final double latitude;
final double longitude;
final DateTime timestamp;
SimplePosition({
required this.latitude,
required this.longitude,
required this.timestamp,
});
}
class GeolocationService { class GeolocationService {
static Future<bool> isLocationServiceEnabled() async { static Future<bool> isLocationServiceEnabled() async {
if (UniversalPlatform.isWeb) return true; // Web no necesita GPS activado
return await Geolocator.isLocationServiceEnabled(); return await Geolocator.isLocationServiceEnabled();
} }
static Future<LocationPermission> checkPermission() async { static Future<LocationPermission> checkPermission() async {
if (UniversalPlatform.isWeb) return LocationPermission.always;
return await Geolocator.checkPermission(); return await Geolocator.checkPermission();
} }
static Future<LocationPermission> requestPermission() async { static Future<LocationPermission> requestPermission() async {
if (UniversalPlatform.isWeb) return LocationPermission.always;
return await Geolocator.requestPermission(); return await Geolocator.requestPermission();
} }
static Future<bool> hasPermission() async { static Future<bool> hasPermission() async {
if (UniversalPlatform.isWeb) return true;
LocationPermission permission = await Geolocator.checkPermission(); LocationPermission permission = await Geolocator.checkPermission();
return permission == LocationPermission.always || return permission == LocationPermission.always ||
permission == LocationPermission.whileInUse; permission == LocationPermission.whileInUse;
} }
static Future<Position?> getCurrentLocation() async { static Future<SimplePosition?> getCurrentLocation() async {
try { try {
// Web usa la API de navegador
if (UniversalPlatform.isWeb) {
return await _getWebLocation();
}
// Móvil: usar menor precisión para más velocidad
bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) { if (!serviceEnabled) return null;
print('❌ GPS desactivado');
return null;
}
LocationPermission permission = await Geolocator.checkPermission(); LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) { if (permission == LocationPermission.denied) {
@@ -48,62 +45,28 @@ class GeolocationService {
if (permission == LocationPermission.deniedForever) return null; if (permission == LocationPermission.deniedForever) return null;
// Usar menor precisión para obtener ubicación más rápido
Position position = await Geolocator.getCurrentPosition( Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low, // ← Cambiado de best a low para más velocidad desiredAccuracy: LocationAccuracy.low,
timeLimit: const Duration(seconds: 5), timeLimit: const Duration(seconds: 5),
); );
return position; return SimplePosition(
latitude: position.latitude,
longitude: position.longitude,
timestamp: position.timestamp,
);
} catch (e) { } catch (e) {
print('Error: $e'); print('Error: $e');
return null; return null;
} }
} }
// Método específico para web static Future<SimplePosition?> getCurrentLocationWithRetry({int maxRetries = 2}) async {
static Future<Position?> _getWebLocation() async {
try {
// Usar la API de geolocalización del navegador
final GeolocationPosition position = await _getWebPosition();
return Position(
latitude: position.latitude,
longitude: position.longitude,
timestamp: DateTime.now(),
accuracy: 0,
altitude: 0,
heading: 0,
speed: 0,
speedAccuracy: 0,
altitudeAccuracy: 0,
headingAccuracy: 0,
);
} catch (e) {
print('Error en web: $e');
return null;
}
}
// Placeholder para web - usar HTML5 geolocation
static Future<GeolocationPosition> _getWebPosition() async {
// Implementación simplificada - en realidad usarías js_interop
throw UnimplementedError('Usar package:geolocator_web');
}
static Future<Position?> getCurrentLocationWithRetry({int maxRetries = 3}) async {
for (int i = 0; i < maxRetries; i++) { for (int i = 0; i < maxRetries; i++) {
print('🔄 Intento ${i + 1} de $maxRetries');
final position = await getCurrentLocation(); final position = await getCurrentLocation();
if (position != null) return position; if (position != null) return position;
if (i < maxRetries - 1) await Future.delayed(const Duration(seconds: 1)); if (i < maxRetries - 1) await Future.delayed(const Duration(seconds: 1));
} }
return null; return null;
} }
}
// Clase auxiliar para web
class GeolocationPosition {
final double latitude;
final double longitude;
GeolocationPosition({required this.latitude, required this.longitude});
} }

View File

@@ -1,3 +1,4 @@
// domicilios.dart
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:geolocator/geolocator.dart'; import 'package:geolocator/geolocator.dart';
@@ -132,13 +133,13 @@ class _DomiciliosViewState extends State<DomiciliosView> {
return; return;
} }
final position = await GeolocationService.getCurrentLocation(); final simplePosition = await GeolocationService.getCurrentLocation();
setState(() { setState(() {
_isLoadingLocation = false; _isLoadingLocation = false;
}); });
if (position == null) { if (simplePosition == null) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar( const SnackBar(
content: Text('No se pudo obtener tu ubicación.'), content: Text('No se pudo obtener tu ubicación.'),
@@ -148,10 +149,10 @@ class _DomiciliosViewState extends State<DomiciliosView> {
return; return;
} }
_mostrarDialogoAgregarConUbicacion(position); _mostrarDialogoAgregarConUbicacion(simplePosition.latitude, simplePosition.longitude);
} }
void _mostrarDialogoAgregarConUbicacion(Position position) { void _mostrarDialogoAgregarConUbicacion(double lat, double lng) {
nombreController.clear(); nombreController.clear();
coloniaController.clear(); coloniaController.clear();
calleController.clear(); calleController.clear();
@@ -182,8 +183,8 @@ class _DomiciliosViewState extends State<DomiciliosView> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text('📍 Ubicación obtenida', style: TextStyle(fontSize: 12, color: Colors.green[700])), Text('📍 Ubicación obtenida', style: TextStyle(fontSize: 12, color: Colors.green[700])),
Text('Lat: ${position.latitude.toStringAsFixed(6)}'), Text('Lat: ${lat.toStringAsFixed(6)}'),
Text('Lng: ${position.longitude.toStringAsFixed(6)}'), Text('Lng: ${lng.toStringAsFixed(6)}'),
], ],
), ),
), ),
@@ -213,7 +214,7 @@ class _DomiciliosViewState extends State<DomiciliosView> {
Expanded( Expanded(
child: ElevatedButton( child: ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: colorAzul, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15))), style: ElevatedButton.styleFrom(backgroundColor: colorAzul, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15))),
onPressed: () => _agregarDomicilio(latitud: position.latitude, longitud: position.longitude), onPressed: () => _agregarDomicilio(latitud: lat, longitud: lng),
child: const Text('Agregar', style: TextStyle(color: Colors.white)), child: const Text('Agregar', style: TextStyle(color: Colors.white)),
), ),
), ),

View File

@@ -1,7 +1,6 @@
// src/views/mapa_expandible.dart
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:latlong2/latlong.dart';
import 'package:universal_platform/universal_platform.dart';
import 'rutas.dart'; import 'rutas.dart';
class MapaExpandible extends StatefulWidget { class MapaExpandible extends StatefulWidget {
@@ -22,20 +21,48 @@ class MapaExpandible extends StatefulWidget {
class _MapaExpandibleState extends State<MapaExpandible> { class _MapaExpandibleState extends State<MapaExpandible> {
bool _isExpanded = false; bool _isExpanded = false;
bool _hasError = false;
bool get _isValidLatLng {
final lat = widget.latitud;
final lng = widget.longitud;
return lat.isFinite && lng.isFinite && lat != 0 && lng != 0;
}
Future<void> _abrirGoogleMaps() async {
final url = 'https://www.google.com/maps?q=${widget.latitud},${widget.longitud}&z=15';
try {
if (await canLaunch(url)) {
await launch(url);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No se pudo abrir el mapa'), backgroundColor: Colors.red),
);
}
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Verificar coordenadas válidas if (!_isValidLatLng) {
final isValid = widget.latitud.isFinite && widget.longitud.isFinite; return Container(
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(20),
),
child: const Text('Ubicación no disponible', style: TextStyle(fontSize: 12)),
);
}
return Column( return Column(
mainAxisSize: MainAxisSize.min,
children: [ children: [
GestureDetector( GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
_isExpanded = !_isExpanded; _isExpanded = !_isExpanded;
_hasError = false;
}); });
}, },
child: Container( child: Container(
@@ -55,11 +82,7 @@ class _MapaExpandibleState extends State<MapaExpandible> {
const SizedBox(width: 4), const SizedBox(width: 4),
Text( Text(
_isExpanded ? 'Ocultar mapa' : 'Ver mapa', _isExpanded ? 'Ocultar mapa' : 'Ver mapa',
style: TextStyle( style: TextStyle(color: colorAzul, fontSize: 12, fontWeight: FontWeight.w500),
color: colorAzul,
fontSize: 12,
fontWeight: FontWeight.w500,
),
), ),
], ],
), ),
@@ -68,66 +91,42 @@ class _MapaExpandibleState extends State<MapaExpandible> {
if (_isExpanded) if (_isExpanded)
Padding( Padding(
padding: const EdgeInsets.only(top: 12), padding: const EdgeInsets.only(top: 12),
child: Container( child: GestureDetector(
height: 250, onTap: _abrirGoogleMaps,
decoration: BoxDecoration( child: Container(
borderRadius: BorderRadius.circular(15), width: 200, // Ancho fijo en lugar de infinity
border: Border.all(color: colorAzul.withOpacity(0.3), width: 1), height: 160,
), decoration: BoxDecoration(
child: _hasError borderRadius: BorderRadius.circular(15),
? Center( border: Border.all(color: colorAzul.withOpacity(0.3), width: 1),
color: Colors.grey[100],
),
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Icon(Icons.map_outlined, size: 50, color: Colors.grey), Icon(Icons.map, size: 40, color: colorAzul),
const SizedBox(height: 10), const SizedBox(height: 8),
Text( Text(
'No se pudo cargar el mapa', 'Ver en Google Maps',
style: TextStyle(color: Colors.grey[600]), style: TextStyle(color: colorAzul, fontWeight: FontWeight.bold, fontSize: 14),
), ),
const SizedBox(height: 5), const SizedBox(height: 4),
Text( Text(
'Coordenadas: ${widget.latitud.toStringAsFixed(4)}, ${widget.longitud.toStringAsFixed(4)}', '${widget.latitud.toStringAsFixed(4)}, ${widget.longitud.toStringAsFixed(4)}',
style: TextStyle(fontSize: 12, color: Colors.grey[500]), style: TextStyle(fontSize: 11, color: Colors.grey[600]),
), ),
], const SizedBox(height: 8),
), Container(
) padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 3),
: ClipRRect( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: colorAzul,
child: FlutterMap( borderRadius: BorderRadius.circular(12),
options: MapOptions(
initialCenter: LatLng(
isValid ? widget.latitud : 20.5111,
isValid ? widget.longitud : -100.9037,
),
initialZoom: 15,
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'com.example.app',
errorCallback: (error, stackTrace) {
setState(() {
_hasError = true;
});
},
),
if (isValid)
MarkerLayer(
markers: [
Marker(
point: LatLng(widget.latitud, widget.longitud),
width: 40,
height: 40,
child: const Icon(
Icons.location_pin,
color: Colors.red,
size: 40,
),
),
],
), ),
child: const Text(
'Tocar para abrir',
style: TextStyle(color: Colors.white, fontSize: 11),
),
),
], ],
), ),
), ),