coordernadas

This commit is contained in:
Juan Felipe Duarte
2023-06-16 15:42:07 -05:00
parent b7b246b03a
commit f69201e0dd
4 changed files with 93 additions and 30 deletions
@@ -176,6 +176,20 @@ class AuthenticationRepository extends GetxController {
return city; return city;
} }
Future<String> getCoordsOfCity(String uid) async {
String coords = '';
try {
final snapshot =
await FirebaseFirestore.instance.collection('users').doc(uid).get();
final Map<String, dynamic>? data = snapshot.data();
coords = data?['coordsOfCity'] ?? '';
} catch (e) {
print('Error getting coords of city: $e');
}
print('Error getting coords of city: $coords');
return coords;
}
Future<String> getAddress(String uid) async { Future<String> getAddress(String uid) async {
String address = ''; String address = '';
try { try {
+32 -14
View File
@@ -17,13 +17,20 @@ final CollectionReference countriesCollection =
class City { class City {
String? cityName; String? cityName;
String? coordsOfCity;
String? stateOfCity; String? stateOfCity;
String? countryOfCity; String? countryOfCity;
City({this.cityName, this.stateOfCity, this.countryOfCity}); City({
this.cityName,
this.coordsOfCity,
this.stateOfCity,
this.countryOfCity,
});
@override @override
String toString() { String toString() {
return "${cityName ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}"; return "${cityName ?? ""}, ${coordsOfCity ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}";
} }
} }
@@ -35,27 +42,27 @@ Future<List<City>> getCountries() async {
for (DocumentSnapshot country in countries.docs) { for (DocumentSnapshot country in countries.docs) {
String countryName = country.id; String countryName = country.id;
Map<String, dynamic> data = country.data() as Map<String, dynamic>; Map<String, dynamic> data = country.data() as Map<String, dynamic>;
Map<String, List<String>> states = {}; Map<String, Map<String, String>> states = {};
for (var entry in data.entries) { for (var entry in data.entries) {
String key = entry.key; String key = entry.key;
List<String> value = List<String>.from(entry.value); Map<String, String> cityData = Map<String, String>.from(entry.value);
states[key] = value; states[key] = cityData;
} }
for (var state in states.entries) { for (var state in states.entries) {
var citysState = state.value var citysState = state.value.entries.map((city) => City(
.map((cityName) => City( cityName: city.key,
cityName: cityName, coordsOfCity: city.value,
stateOfCity: state.key, stateOfCity: state.key,
countryOfCity: countryName)) countryOfCity: countryName,
.toList(); ));
citys.addAll(citysState); citys.addAll(citysState);
} }
} }
} catch (e) { } catch (e) {
print('xd $e'); print('xd seqe $e');
} }
return citys; return citys;
@@ -98,13 +105,18 @@ class _CityScreenState extends State<CityScreen> {
} }
} }
Future<void> updateCity(String cityName) async { Future<void> updateCity(String cityName, String coordsCity) async {
try { try {
await FirebaseFirestore.instance await FirebaseFirestore.instance
.collection('users') .collection('users')
.doc(uid) .doc(uid)
.update({'city': cityName}); .update({'city': cityName});
await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.update({'coordsOfCity': coordsCity});
print('Ciudad actualizada correctamente'); print('Ciudad actualizada correctamente');
} catch (e) { } catch (e) {
try { try {
@@ -112,6 +124,11 @@ class _CityScreenState extends State<CityScreen> {
.collection('users') .collection('users')
.doc(uid) .doc(uid)
.set({'city': cityName}); .set({'city': cityName});
await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.set({'coordsOfCity': coordsCity});
} catch (e) { } catch (e) {
print('Error al agregar la ciudad: $e'); print('Error al agregar la ciudad: $e');
} }
@@ -175,7 +192,8 @@ class _CityScreenState extends State<CityScreen> {
), ),
), ),
onTap: () { onTap: () {
updateCity(citys[index].cityName ?? ""); updateCity(citys[index].cityName ?? "",
citys[index].coordsOfCity ?? "");
Navigator.pop(context, citys[index].cityName ?? ""); Navigator.pop(context, citys[index].cityName ?? "");
}, },
); );
+19 -9
View File
@@ -13,7 +13,6 @@ import 'package:prosappco/src/authentication/authentication_repository.dart';
import 'package:prosappco/src/components/drawer_menu.dart'; import 'package:prosappco/src/components/drawer_menu.dart';
import 'package:prosappco/src/models/event_model.dart'; import 'package:prosappco/src/models/event_model.dart';
import 'package:prosappco/src/models/user_model.dart'; import 'package:prosappco/src/models/user_model.dart';
import 'package:prosappco/src/screens/local_notification.dart';
import 'package:prosappco/src/screens/professional.dart'; import 'package:prosappco/src/screens/professional.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:prosappco/src/screens/profile.dart'; import 'package:prosappco/src/screens/profile.dart';
@@ -186,6 +185,10 @@ class _ServiceScreenState extends State<ServiceScreen> {
late String lat; late String lat;
late String long; late String long;
double latUser = 0.0;
double lngUser = 0.0;
var coordinates; var coordinates;
Future<String> getLocationName(double latitude, double longitude) async { Future<String> getLocationName(double latitude, double longitude) async {
@@ -239,19 +242,24 @@ class _ServiceScreenState extends State<ServiceScreen> {
controller: _ubicationController, controller: _ubicationController,
readOnly: true, readOnly: true,
onTap: () async { onTap: () async {
final dynamic datos = await Navigator.push( final List<dynamic> datos = await Navigator.push(
context, context,
CupertinoPageRoute( CupertinoPageRoute(
builder: (BuildContext context) { builder: (BuildContext context) {
return UbicacionScreen(); return const UbicacionScreen();
}, },
), ),
); );
print(datos);
if (datos != null) { if (datos.length == 3) {
final formattedAddress = datos[0];
final lat = datos[1];
final lng = datos[2];
setState(() { setState(() {
_ubicationController.text = datos; _ubicationController.text = formattedAddress;
print(_ubicationController.text); latUser = lat;
lngUser = lng;
}); });
} }
}, },
@@ -291,6 +299,8 @@ class _ServiceScreenState extends State<ServiceScreen> {
if (ubicacion == 'sitio') { if (ubicacion == 'sitio') {
professionalAddress = profesional.realAddress; professionalAddress = profesional.realAddress;
_ubicationController.text = profesional.realAddress; _ubicationController.text = profesional.realAddress;
latUser = profesional.latitude;
lngUser = profesional.longitude;
} }
}); });
} }
@@ -419,8 +429,8 @@ class _ServiceScreenState extends State<ServiceScreen> {
professionalId, professionalId,
ubicacion, ubicacion,
professionalAddress, professionalAddress,
0.0, latUser,
0.0, lngUser,
'pendiente', 'pendiente',
professionalTarifa == 0 ? 0 : professionalTarifa, professionalTarifa == 0 ? 0 : professionalTarifa,
false, false,
+28 -7
View File
@@ -1,9 +1,10 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:prosappco/src/components/network_utility.dart'; import 'package:prosappco/src/components/network_utility.dart';
import 'package:prosappco/src/components/pop_appbar.dart'; import 'package:prosappco/src/components/pop_appbar.dart';
import '../authentication/authentication_repository.dart';
class UbicacionScreen extends StatefulWidget { class UbicacionScreen extends StatefulWidget {
const UbicacionScreen({super.key}); const UbicacionScreen({super.key});
@@ -14,17 +15,34 @@ class UbicacionScreen extends StatefulWidget {
class _UbicacionScreenState extends State<UbicacionScreen> { class _UbicacionScreenState extends State<UbicacionScreen> {
List<dynamic> _placesList = []; List<dynamic> _placesList = [];
String selectedPlace = ''; String selectedPlace = '';
String _coordsOfCity = '0.0,0.0';
@override
void initState() {
super.initState();
final uid = AuthenticationRepository.instance.getCurrentUserUid();
if (_coordsOfCity == '0.0,0.0') {
AuthenticationRepository.instance
.getCoordsOfCity(uid.toString())
.then((String s) => setState(() {
_coordsOfCity = s;
}));
}
}
void placeAutoComplete(String query) async { void placeAutoComplete(String query) async {
Uri uri = Uri.https("admin.prosapp.co", "/autocomplete", { Uri uri = Uri.https("admin.prosapp.co", "/autocomplete", {
"input": query, "input": query,
"location": _coordsOfCity,
}); });
String? response = await NetworkUtility.fetchUrl(uri); String? response = await NetworkUtility.fetchUrl(uri);
if (response != null) { if (response != null) {
setState(() { setState(() {
_placesList = jsonDecode(response.toString())['predictions']; _placesList = jsonDecode(response.toString())['results'];
}); });
} }
} }
@@ -38,7 +56,7 @@ class _UbicacionScreenState extends State<UbicacionScreen> {
}, },
label: 'Tu ubicación'), label: 'Tu ubicación'),
body: Center( body: Center(
child: Container( child: SizedBox(
width: 300, width: 300,
child: Column( child: Column(
children: [ children: [
@@ -60,10 +78,13 @@ class _UbicacionScreenState extends State<UbicacionScreen> {
itemBuilder: (context, index) { itemBuilder: (context, index) {
return ListTile( return ListTile(
onTap: () { onTap: () {
Navigator.pop( Navigator.pop(context, [
context, _placesList[index]['description']); _placesList[index]['formatted_address'],
_placesList[index]['geometry']['location']['lat'],
_placesList[index]['geometry']['location']['lng']
]);
}, },
title: Text(_placesList[index]['description']), title: Text(_placesList[index]['formatted_address']),
); );
}, },
), ),