From 6464161b3423a08da94a0b32e7b019fc75006ad7 Mon Sep 17 00:00:00 2001 From: Benjamin Ramhorst Date: Mon, 14 Feb 2022 16:59:33 +0000 Subject: [PATCH] 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: