Added reverse geocoder

This commit is contained in:
vjrj 2018-06-11 10:06:28 +02:00
parent ef257fcfe1
commit 662deb3086
4 changed files with 81 additions and 45 deletions

View file

@ -33,14 +33,12 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
? loc.description ? loc.description
: 'Position: ${loc.lat}, ${loc.lon}'; : 'Position: ${loc.lat}, ${loc.lon}';
return new ListTile( return new ListTile(
dense: false, dense: true,
leading: const Icon(Icons.location_on), leading: const Icon(Icons.location_on),
// trailing: const Icon(Icons.delete), trailing: const Icon(Icons.notifications_off),
title: new Text(desc), title: new Text(desc),
onLongPress: () { onLongPress: () {
_scaffoldKey.currentState.showSnackBar(new SnackBar( showSnackMsg('Slide horizontally to delete this location');
content: new Text('Slide horizontally to delete this location'),
));
}, },
onTap: () { onTap: () {
const String km = '100'; const String km = '100';
@ -61,6 +59,12 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
}); });
} }
void showSnackMsg(String msg) {
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text(msg),
));
}
Widget _buildSavedLocations() { Widget _buildSavedLocations() {
return new ListView.builder( return new ListView.builder(
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
@ -172,8 +176,7 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
backColor: fires600), backColor: fires600),
])), ])),
floatingActionButton: globals.yourLocations.length > 0 floatingActionButton: globals.yourLocations.length > 0
? Column( ? Column(mainAxisAlignment: MainAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
// crossAxisAlignment: CrossAxisAlignment.center, // crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
FloatingActionButton.extended( FloatingActionButton.extended(
@ -210,11 +213,15 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
void _saveLocation(Future<BasicLocation> location) { void _saveLocation(Future<BasicLocation> location) {
location.then((newLocation) { location.then((newLocation) {
if (newLocation != BasicLocation.noLocation) if (newLocation != BasicLocation.noLocation) {
this.setState(() { if (globals.yourLocations.contains(newLocation)) {
globals.yourLocations.add(newLocation); showSnackMsg('You have already added this location');
persist(); } else
}); this.setState(() {
globals.yourLocations.add(newLocation);
persist();
});
}
}); });
} }
} }

View file

@ -1,25 +1,28 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class BasicLocation { class BasicLocation extends Comparable<BasicLocation> {
final double lat; final double lat;
final double lon; 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}); 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<String, dynamic> json) BasicLocation.fromJson(Map<String, dynamic> json)
: lat = json['lat'], : lat = json['lat'],
lon = json['lon'], lon = json['lon'],
description = json['description'] description = json['description'];
;
Map<String, dynamic> toJson() =>
{
'lat': lat,
'lon': lon,
'description': description,
};
Map<String, dynamic> toJson() => {
'lat': lat,
'lon': lon,
'description': description,
};
} }

View file

@ -3,28 +3,53 @@ import 'package:flutter/services.dart';
import 'dart:async'; import 'dart:async';
import 'basicLocation.dart'; import 'basicLocation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'globals.dart' as globals;
Future<BasicLocation> getUserLocation(GlobalKey<ScaffoldState> scaffoldKey) async { Future<BasicLocation> getUserLocation(
// Platform messages may fail, so we use a try/catch PlatformException. GlobalKey<ScaffoldState> scaffoldKey) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
Location _location = new Location();
Map<String, double> 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 { try {
Location _location = new Location(); address = await getReverseLocation(basicLocation);
Map<String, double> location = await _location.getLocation; basicLocation.description = address;
// print('location $location'); } catch (e) {
try {
// It seems that the lib fails with lat/lon values address = await getReverseLocation(basicLocation, true);
return new BasicLocation( basicLocation.description = address;
lat: location['latitude'], lon: location['longitude']); } catch (e) {
} on PlatformException catch (e) { print('We cannot reverse geolocate');
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;
} }
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<String> 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;
}

View file

@ -22,6 +22,7 @@ dependencies:
flutter_map: "^0.0.1" flutter_map: "^0.0.1"
http: "^0.11.3+16" http: "^0.11.3+16"
simple_moment: "^0.0.3" simple_moment: "^0.0.3"
geocoder: "^0.0.1"
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: