From 5431d765d4cb2ecc7211c8fc9d1ab48c805c0802 Mon Sep 17 00:00:00 2001 From: Benjamin Ramhorst Date: Mon, 14 Feb 2022 14:40:18 +0000 Subject: [PATCH 1/3] Added marker on map to show pet location --- lib/home.dart | 2 +- lib/model/latitude_longitude.dart | 10 ++ lib/service/map_service.dart | 21 +++++ lib/utils/endpoints.dart | 2 + lib/view/{ => map}/map_page.dart | 21 ++++- pubspec.lock | 147 ++++++++++++++++++++++++++++++ pubspec.yaml | 1 + 7 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 lib/model/latitude_longitude.dart create mode 100644 lib/service/map_service.dart rename lib/view/{ => map}/map_page.dart (53%) diff --git a/lib/home.dart b/lib/home.dart index 7599e0c..6a3b31d 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:leg_barkr_app/view/metrics/metrics_page.dart'; import 'package:leg_barkr_app/view/steps/steps_page.dart'; -import 'package:leg_barkr_app/view/map_page.dart'; +import 'package:leg_barkr_app/view/map/map_page.dart'; import 'package:leg_barkr_app/view/settings_page.dart'; class HomeScreen extends StatefulWidget { diff --git a/lib/model/latitude_longitude.dart b/lib/model/latitude_longitude.dart new file mode 100644 index 0000000..697b1d4 --- /dev/null +++ b/lib/model/latitude_longitude.dart @@ -0,0 +1,10 @@ +class LatitudeLongitude { + double latitude; + double longitude; + + LatitudeLongitude(this.latitude, this.longitude); + + factory LatitudeLongitude.fromJson(Map parsedJson){ + return LatitudeLongitude(parsedJson['latitude'], parsedJson['longitude']); + } +} diff --git a/lib/service/map_service.dart b/lib/service/map_service.dart new file mode 100644 index 0000000..b7fbf36 --- /dev/null +++ b/lib/service/map_service.dart @@ -0,0 +1,21 @@ +import 'dart:convert'; + +import 'package:http/http.dart' as http; +import 'package:leg_barkr_app/model/latitude_longitude.dart'; +import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; + + +class MapService{ + Future getLastLocation(deviceId) async { + final response = await http.get( + Uri.parse(Endpoints.getLastLocation), + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + 'deviceid': deviceId, + }, + ); + print(response.body); + return LatitudeLongitude.fromJson(jsonDecode(response.body)); + + } +} \ No newline at end of file diff --git a/lib/utils/endpoints.dart b/lib/utils/endpoints.dart index bb342ec..932f9ec 100644 --- a/lib/utils/endpoints.dart +++ b/lib/utils/endpoints.dart @@ -1,3 +1,5 @@ const String home = "https://leg-barkr.nw.r.appspot.com/"; const String register = "https://leg-barkr.nw.r.appspot.com/authentication/register"; const String verify = "https://leg-barkr.nw.r.appspot.com/authentication/verify"; +const String getLastLocation = "https://leg-barkr.nw.r.appspot.com/readings/last/location"; + diff --git a/lib/view/map_page.dart b/lib/view/map/map_page.dart similarity index 53% rename from lib/view/map_page.dart rename to lib/view/map/map_page.dart index c7b967e..77deade 100644 --- a/lib/view/map_page.dart +++ b/lib/view/map/map_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; +import 'package:leg_barkr_app/service/map_service.dart'; class MapPage extends StatefulWidget { const MapPage({ Key? key }) : super(key: key); @@ -14,8 +15,23 @@ class _MapPageState extends State { // This will be changed, to center around the dog (once app reads metrics from the server) final LatLng _center = const LatLng(51.498356, -0.176894); - void _onMapCreated(GoogleMapController mapController) { - _mapController = mapController; + final Map _markers = {}; + + Future _onMapCreated(GoogleMapController controller) async { + final lastLocation = await MapService().getLastLocation("132-567-001"); // change this. + setState(() { + _markers.clear(); + print(lastLocation.longitude); + final petMarker = Marker( + markerId: MarkerId("pet_location"), + position: LatLng(lastLocation.latitude, lastLocation.longitude), + infoWindow: InfoWindow( + title: "Pet location", + ), + ); + _markers["pet_location"] = petMarker; + print(_markers["pet_location"]); + }); } @override @@ -28,6 +44,7 @@ class _MapPageState extends State { target: _center, zoom: 16.0, ), + markers: _markers.values.toSet(), ), ), ); diff --git a/pubspec.lock b/pubspec.lock index b95cc4d..c1dbae2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,27 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + url: "https://pub.dartlang.org" + source: hosted + version: "34.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.0" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" async: dependency: transitive description: @@ -15,6 +36,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + build: + dependency: transitive + description: + name: build + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.1" + build_config: + dependency: transitive + description: + name: build_config + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" characters: dependency: transitive description: @@ -43,6 +78,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.12.0" + checked_yaml: + dependency: transitive + description: + name: checked_yaml + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + cli_util: + dependency: transitive + description: + name: cli_util + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.5" clock: dependency: transitive description: @@ -57,6 +106,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.15.0" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" csslib: dependency: transitive description: @@ -71,6 +134,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + dart_style: + dependency: transitive + description: + name: dart_style + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.1" fake_async: dependency: transitive description: @@ -78,6 +148,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.2" firebase_auth: dependency: "direct main" description: @@ -149,6 +226,13 @@ packages: description: flutter source: sdk version: "0.0.0" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" google_maps: dependency: transitive description: @@ -219,6 +303,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.7.4" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "4.4.0" + json_serializable: + dependency: "direct main" + description: + name: json_serializable + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.4" lints: dependency: transitive description: @@ -254,6 +352,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.7.0" + package_config: + dependency: transitive + description: + name: package_config + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" path: dependency: transitive description: @@ -275,6 +380,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + pub_semver: + dependency: transitive + description: + name: pub_semver + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + pubspec_parse: + dependency: transitive + description: + name: pubspec_parse + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" sanitize_html: dependency: transitive description: @@ -287,6 +406,20 @@ packages: description: flutter source: sdk version: "0.0.99" + source_gen: + dependency: transitive + description: + name: source_gen + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + source_helper: + dependency: transitive + description: + name: source_helper + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" source_span: dependency: transitive description: @@ -364,6 +497,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + watcher: + dependency: transitive + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + yaml: + dependency: transitive + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" sdks: dart: ">=2.16.0 <3.0.0" flutter: ">=2.5.0" diff --git a/pubspec.yaml b/pubspec.yaml index 6e6afc0..498504f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: http: ^0.13.4 firebase_core: ^1.12.0 firebase_auth: ^3.3.7 + json_serializable: ^6.1.4 dev_dependencies: From 6464161b3423a08da94a0b32e7b019fc75006ad7 Mon Sep 17 00:00:00 2001 From: Benjamin Ramhorst Date: Mon, 14 Feb 2022 16:59:33 +0000 Subject: [PATCH 2/3] Complete map functionality --- android/app/src/main/AndroidManifest.xml | 1 + ios/Runner/Info.plist | 2 + lib/service/map_service.dart | 15 +++++- lib/view/map/map_page.dart | 45 +++++++++++------- pubspec.lock | 58 +++++++++++++++++++++++- pubspec.yaml | 2 + 6 files changed, 104 insertions(+), 19 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 1dd97e7..6237088 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + LaunchScreen UIMainStoryboardFile Main + NSLocationWhenInUseUsageDescription + This app needs access to location when open. UISupportedInterfaceOrientations UIInterfaceOrientationPortrait diff --git a/lib/service/map_service.dart b/lib/service/map_service.dart index b7fbf36..2b63ab5 100644 --- a/lib/service/map_service.dart +++ b/lib/service/map_service.dart @@ -1,5 +1,5 @@ import 'dart:convert'; - +import 'package:geolocator/geolocator.dart'; import 'package:http/http.dart' as http; import 'package:leg_barkr_app/model/latitude_longitude.dart'; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; @@ -16,6 +16,19 @@ class MapService{ ); print(response.body); return LatitudeLongitude.fromJson(jsonDecode(response.body)); + } + + Future getMyLocation() async{ + LocationPermission permission; + permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + if (permission == LocationPermission.deniedForever) { + throw Exception('Location denied'); + } + } + return await Geolocator.getCurrentPosition(); } + } \ No newline at end of file diff --git a/lib/view/map/map_page.dart b/lib/view/map/map_page.dart index 77deade..e0d3b63 100644 --- a/lib/view/map/map_page.dart +++ b/lib/view/map/map_page.dart @@ -11,26 +11,37 @@ class MapPage extends StatefulWidget { class _MapPageState extends State { late GoogleMapController _mapController; - - // This will be changed, to center around the dog (once app reads metrics from the server) - final LatLng _center = const LatLng(51.498356, -0.176894); - final Map _markers = {}; Future _onMapCreated(GoogleMapController controller) async { - final lastLocation = await MapService().getLastLocation("132-567-001"); // change this. + _mapController = controller; + final lastLocation = await MapService().getLastLocation("132-567-001"); // change this. + final myLocation = await MapService().getMyLocation(); + print(myLocation.latitude); setState(() { _markers.clear(); - print(lastLocation.longitude); - final petMarker = Marker( - markerId: MarkerId("pet_location"), - position: LatLng(lastLocation.latitude, lastLocation.longitude), - infoWindow: InfoWindow( - title: "Pet location", - ), - ); - _markers["pet_location"] = petMarker; - print(_markers["pet_location"]); + if (lastLocation.latitude!=-1.0 && lastLocation.latitude!=-1.0){ + final petMarker = Marker( + markerId: MarkerId("pet_location"), + position: LatLng(lastLocation.latitude, lastLocation.longitude), + infoWindow: InfoWindow( + title: "Pet location", + ), + ); + _markers["pet_location"] = petMarker; + } + + if (myLocation.latitude!=-1.0 && myLocation.longitude!=-1.0){ + final myMarker = Marker( + markerId: MarkerId("my_location"), + position: LatLng(myLocation.latitude, myLocation.longitude), + infoWindow: InfoWindow( + title: "My location", + ), + ); + _markers["my_location"] = myMarker; + } + _mapController.animateCamera(CameraUpdate.newLatLng(LatLng(myLocation.latitude, myLocation.longitude))); }); } @@ -41,8 +52,8 @@ class _MapPageState extends State { body: GoogleMap( onMapCreated: _onMapCreated, initialCameraPosition: CameraPosition( - target: _center, - zoom: 16.0, + target: LatLng(51.5, -0.12), + zoom: 12.0, ), markers: _markers.values.toSet(), ), diff --git a/pubspec.lock b/pubspec.lock index c1dbae2..1384262 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -226,6 +226,62 @@ packages: description: flutter source: sdk version: "0.0.0" + geocoding: + dependency: "direct main" + description: + name: geocoding + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" + geocoding_platform_interface: + dependency: transitive + description: + name: geocoding_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + geolocator: + dependency: "direct main" + description: + name: geolocator + url: "https://pub.dartlang.org" + source: hosted + version: "8.2.0" + geolocator_android: + dependency: transitive + description: + name: geolocator_android + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" + geolocator_apple: + dependency: transitive + description: + name: geolocator_apple + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1+1" + geolocator_platform_interface: + dependency: transitive + description: + name: geolocator_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.3" + geolocator_web: + dependency: transitive + description: + name: geolocator_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.4" + geolocator_windows: + dependency: transitive + description: + name: geolocator_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" glob: dependency: transitive description: @@ -513,4 +569,4 @@ packages: version: "3.1.0" sdks: dart: ">=2.16.0 <3.0.0" - flutter: ">=2.5.0" + flutter: ">=2.8.0" diff --git a/pubspec.yaml b/pubspec.yaml index 498504f..eb22923 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,6 +42,8 @@ dependencies: firebase_core: ^1.12.0 firebase_auth: ^3.3.7 json_serializable: ^6.1.4 + geocoding: ^2.0.2 + geolocator: ^8.2.0 dev_dependencies: From c929242804c89014a6cb7a407482e95e8529f716 Mon Sep 17 00:00:00 2001 From: Benjamin Ramhorst Date: Mon, 14 Feb 2022 17:03:32 +0000 Subject: [PATCH 3/3] Code refractoring --- lib/service/map_service.dart | 4 +--- lib/utils/resources.dart | 7 ------- lib/view/map/map_page.dart | 34 ++++++++++++---------------------- 3 files changed, 13 insertions(+), 32 deletions(-) delete mode 100644 lib/utils/resources.dart diff --git a/lib/service/map_service.dart b/lib/service/map_service.dart index 2b63ab5..b90f09c 100644 --- a/lib/service/map_service.dart +++ b/lib/service/map_service.dart @@ -4,9 +4,8 @@ import 'package:http/http.dart' as http; import 'package:leg_barkr_app/model/latitude_longitude.dart'; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; - class MapService{ - Future getLastLocation(deviceId) async { + Future getPetLastLocation(deviceId) async { final response = await http.get( Uri.parse(Endpoints.getLastLocation), headers: { @@ -14,7 +13,6 @@ class MapService{ 'deviceid': deviceId, }, ); - print(response.body); return LatitudeLongitude.fromJson(jsonDecode(response.body)); } diff --git a/lib/utils/resources.dart b/lib/utils/resources.dart deleted file mode 100644 index 4b038a8..0000000 --- a/lib/utils/resources.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'package:flutter/material.dart'; - -class Resources { - static final Color primaryColour = Colors.green; - static final Color primaryAccentColour = Colors.greenAccent; - -} \ No newline at end of file diff --git a/lib/view/map/map_page.dart b/lib/view/map/map_page.dart index e0d3b63..b305560 100644 --- a/lib/view/map/map_page.dart +++ b/lib/view/map/map_page.dart @@ -15,32 +15,22 @@ class _MapPageState extends State { Future _onMapCreated(GoogleMapController controller) async { _mapController = controller; - final lastLocation = await MapService().getLastLocation("132-567-001"); // change this. + final lastLocation = await MapService().getPetLastLocation("132-567-001"); // change this. final myLocation = await MapService().getMyLocation(); - print(myLocation.latitude); setState(() { _markers.clear(); - if (lastLocation.latitude!=-1.0 && lastLocation.latitude!=-1.0){ - final petMarker = Marker( - markerId: MarkerId("pet_location"), - position: LatLng(lastLocation.latitude, lastLocation.longitude), - infoWindow: InfoWindow( - title: "Pet location", - ), - ); - _markers["pet_location"] = petMarker; - } + final petMarker = Marker( + markerId: MarkerId("pet_location"), + position: LatLng(lastLocation.latitude, lastLocation.longitude), + infoWindow: InfoWindow(title: "Pet location")); - if (myLocation.latitude!=-1.0 && myLocation.longitude!=-1.0){ - final myMarker = Marker( - markerId: MarkerId("my_location"), - position: LatLng(myLocation.latitude, myLocation.longitude), - infoWindow: InfoWindow( - title: "My location", - ), - ); - _markers["my_location"] = myMarker; - } + final myMarker = Marker( + markerId: MarkerId("my_location"), + position: LatLng(myLocation.latitude, myLocation.longitude), + infoWindow: InfoWindow(title: "My location")); + + _markers["pet_location"] = petMarker; + _markers["my_location"] = myMarker; _mapController.animateCamera(CameraUpdate.newLatLng(LatLng(myLocation.latitude, myLocation.longitude))); }); }