Complete map functionality

This commit is contained in:
Benjamin Ramhorst 2022-02-14 16:59:33 +00:00
parent 5431d765d4
commit 6464161b34
6 changed files with 104 additions and 19 deletions

View file

@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.legbarkr.leg_barkr_app"> package="com.legbarkr.leg_barkr_app">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application <application
android:label="leg_barkr_app" android:label="leg_barkr_app"
android:name="${applicationName}" android:name="${applicationName}"

View file

@ -28,6 +28,8 @@
<string>LaunchScreen</string> <string>LaunchScreen</string>
<key>UIMainStoryboardFile</key> <key>UIMainStoryboardFile</key>
<string>Main</string> <string>Main</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>UISupportedInterfaceOrientations</key> <key>UISupportedInterfaceOrientations</key>
<array> <array>
<string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortrait</string>

View file

@ -1,5 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:leg_barkr_app/model/latitude_longitude.dart'; import 'package:leg_barkr_app/model/latitude_longitude.dart';
import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints;
@ -16,6 +16,19 @@ class MapService{
); );
print(response.body); print(response.body);
return LatitudeLongitude.fromJson(jsonDecode(response.body)); return LatitudeLongitude.fromJson(jsonDecode(response.body));
}
Future<Position> 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();
} }
} }

View file

@ -11,17 +11,16 @@ class MapPage extends StatefulWidget {
class _MapPageState extends State<MapPage> { class _MapPageState extends State<MapPage> {
late GoogleMapController _mapController; 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<String, Marker> _markers = {}; final Map<String, Marker> _markers = {};
Future<void> _onMapCreated(GoogleMapController controller) async { Future<void> _onMapCreated(GoogleMapController controller) async {
_mapController = controller;
final lastLocation = await MapService().getLastLocation("132-567-001"); // change this. final lastLocation = await MapService().getLastLocation("132-567-001"); // change this.
final myLocation = await MapService().getMyLocation();
print(myLocation.latitude);
setState(() { setState(() {
_markers.clear(); _markers.clear();
print(lastLocation.longitude); if (lastLocation.latitude!=-1.0 && lastLocation.latitude!=-1.0){
final petMarker = Marker( final petMarker = Marker(
markerId: MarkerId("pet_location"), markerId: MarkerId("pet_location"),
position: LatLng(lastLocation.latitude, lastLocation.longitude), position: LatLng(lastLocation.latitude, lastLocation.longitude),
@ -30,7 +29,19 @@ class _MapPageState extends State<MapPage> {
), ),
); );
_markers["pet_location"] = petMarker; _markers["pet_location"] = petMarker;
print(_markers["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;
}
_mapController.animateCamera(CameraUpdate.newLatLng(LatLng(myLocation.latitude, myLocation.longitude)));
}); });
} }
@ -41,8 +52,8 @@ class _MapPageState extends State<MapPage> {
body: GoogleMap( body: GoogleMap(
onMapCreated: _onMapCreated, onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition( initialCameraPosition: CameraPosition(
target: _center, target: LatLng(51.5, -0.12),
zoom: 16.0, zoom: 12.0,
), ),
markers: _markers.values.toSet(), markers: _markers.values.toSet(),
), ),

View file

@ -226,6 +226,62 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: glob:
dependency: transitive dependency: transitive
description: description:
@ -513,4 +569,4 @@ packages:
version: "3.1.0" version: "3.1.0"
sdks: sdks:
dart: ">=2.16.0 <3.0.0" dart: ">=2.16.0 <3.0.0"
flutter: ">=2.5.0" flutter: ">=2.8.0"

View file

@ -42,6 +42,8 @@ dependencies:
firebase_core: ^1.12.0 firebase_core: ^1.12.0
firebase_auth: ^3.3.7 firebase_auth: ^3.3.7
json_serializable: ^6.1.4 json_serializable: ^6.1.4
geocoding: ^2.0.2
geolocator: ^8.2.0
dev_dependencies: dev_dependencies: