diff --git a/lib/activeFires.dart b/lib/activeFires.dart index a57a80f..4c3254f 100644 --- a/lib/activeFires.dart +++ b/lib/activeFires.dart @@ -33,14 +33,12 @@ class _ActiveFiresPageState extends State { ? loc.description : 'Position: ${loc.lat}, ${loc.lon}'; return new ListTile( - dense: false, + dense: true, leading: const Icon(Icons.location_on), - // trailing: const Icon(Icons.delete), + trailing: const Icon(Icons.notifications_off), title: new Text(desc), onLongPress: () { - _scaffoldKey.currentState.showSnackBar(new SnackBar( - content: new Text('Slide horizontally to delete this location'), - )); + showSnackMsg('Slide horizontally to delete this location'); }, onTap: () { const String km = '100'; @@ -61,6 +59,12 @@ class _ActiveFiresPageState extends State { }); } + void showSnackMsg(String msg) { + _scaffoldKey.currentState.showSnackBar(new SnackBar( + content: new Text(msg), + )); + } + Widget _buildSavedLocations() { return new ListView.builder( padding: const EdgeInsets.all(16.0), @@ -172,8 +176,7 @@ class _ActiveFiresPageState extends State { backColor: fires600), ])), floatingActionButton: globals.yourLocations.length > 0 - ? Column( - mainAxisAlignment: MainAxisAlignment.end, + ? Column(mainAxisAlignment: MainAxisAlignment.end, // crossAxisAlignment: CrossAxisAlignment.center, children: [ FloatingActionButton.extended( @@ -210,11 +213,15 @@ class _ActiveFiresPageState extends State { void _saveLocation(Future location) { location.then((newLocation) { - if (newLocation != BasicLocation.noLocation) - this.setState(() { - globals.yourLocations.add(newLocation); - persist(); - }); + if (newLocation != BasicLocation.noLocation) { + if (globals.yourLocations.contains(newLocation)) { + showSnackMsg('You have already added this location'); + } else + this.setState(() { + globals.yourLocations.add(newLocation); + persist(); + }); + } }); } } diff --git a/lib/basicLocation.dart b/lib/basicLocation.dart index 7b638bb..040de65 100644 --- a/lib/basicLocation.dart +++ b/lib/basicLocation.dart @@ -1,25 +1,28 @@ import 'package:flutter/material.dart'; -class BasicLocation { +class BasicLocation extends Comparable { final double lat; final double lon; - final String description; + String description; - static BasicLocation noLocation = new BasicLocation(lat:0.0, lon:0.0); + static BasicLocation noLocation = new BasicLocation(lat: 0.0, lon: 0.0); BasicLocation({@required this.lat, @required this.lon, this.description}); + int compareTo(BasicLocation other) { + return lat == other.lat && lon == other.lon ? 1 : 0; + } + + bool operator ==(o) => o is BasicLocation && o.lat == lat && o.lon == lon; + BasicLocation.fromJson(Map json) : lat = json['lat'], - lon = json['lon'], - description = json['description'] - ; - - Map toJson() => - { - 'lat': lat, - 'lon': lon, - 'description': description, - }; + lon = json['lon'], + description = json['description']; + Map toJson() => { + 'lat': lat, + 'lon': lon, + 'description': description, + }; } diff --git a/lib/locationUtils.dart b/lib/locationUtils.dart index 373aada..67cd0a6 100644 --- a/lib/locationUtils.dart +++ b/lib/locationUtils.dart @@ -3,28 +3,53 @@ import 'package:flutter/services.dart'; import 'dart:async'; import 'basicLocation.dart'; import 'package:flutter/material.dart'; +import 'package:geocoder/geocoder.dart'; +import 'globals.dart' as globals; -Future getUserLocation(GlobalKey scaffoldKey) async { - // Platform messages may fail, so we use a try/catch PlatformException. +Future getUserLocation( + GlobalKey scaffoldKey) async { + // Platform messages may fail, so we use a try/catch PlatformException. + try { + Location _location = new Location(); + Map location = await _location.getLocation; + // print('location $location'); + + // It seems that the lib fails with lat/lon values + var basicLocation = new BasicLocation( + lat: location['latitude'], lon: location['longitude']); + var address; try { - Location _location = new Location(); - Map location = await _location.getLocation; - // print('location $location'); - - // It seems that the lib fails with lat/lon values - return new BasicLocation( - lat: location['latitude'], lon: location['longitude']); - } on PlatformException catch (e) { - if (e.code == 'PERMISSION_DENIED') { - scaffoldKey.currentState.showSnackBar(new SnackBar( - content: new Text('We don\'t have permission to get your location'), - )); - } else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') { + address = await getReverseLocation(basicLocation); + basicLocation.description = address; + } catch (e) { + try { + address = await getReverseLocation(basicLocation, true); + basicLocation.description = address; + } catch (e) { + print('We cannot reverse geolocate'); } - scaffoldKey.currentState.showSnackBar(new SnackBar( - content: new Text( - 'I cannot get your current location. It\'s your ubication enabled?'), - )); - return BasicLocation.noLocation; } + return basicLocation; + } on PlatformException catch (e) { + if (e.code == 'PERMISSION_DENIED') { + scaffoldKey.currentState.showSnackBar(new SnackBar( + content: new Text('We don\'t have permission to get your location'), + )); + } else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {} + scaffoldKey.currentState.showSnackBar(new SnackBar( + content: new Text( + 'I cannot get your current location. It\'s your ubication enabled?'), + )); + return BasicLocation.noLocation; } +} + +Future getReverseLocation(BasicLocation loc, + [bool external = false]) async { + final coordinates = new Coordinates(loc.lat, loc.lon); + var geocoder = external ? Geocoder.google(globals.gmapKey) : Geocoder.local; + var addresses = await geocoder.findAddressesFromCoordinates(coordinates); + var first = addresses.first; + print("${first.featureName} : ${first.addressLine}"); + return first.addressLine; +} diff --git a/pubspec.yaml b/pubspec.yaml index f90fe44..1c47178 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,6 +22,7 @@ dependencies: flutter_map: "^0.0.1" http: "^0.11.3+16" simple_moment: "^0.0.3" + geocoder: "^0.0.1" dev_dependencies: flutter_test: