From 98ef6639c50eddb02a4ec63b1577d4dd37a94b9a Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Sun, 6 Dec 2020 14:41:54 +0100 Subject: [PATCH] goelocation finished --- lib/components/mapView.dart | 91 ++++++++++++++++++------------------- lib/services.dart | 8 ++++ 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/lib/components/mapView.dart b/lib/components/mapView.dart index 58b5ef9..202c12b 100644 --- a/lib/components/mapView.dart +++ b/lib/components/mapView.dart @@ -14,18 +14,13 @@ class MapViewState extends State { MenuiServices services = new MenuiServices(); Position position; - Future> createMarkers() async { + Future createMarkers() async { Map markers = {}; Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high); - setState(() { - position = position; - }); - print('position is set'); - /* List restaurants = await services.fetchRestaurantsByLocation( - position.latitude, position.longitude, 1000); */ - List restaurants = - await services.fetchRestaurantsByLocation(20.60912, 52.87728); + LatLng location = new LatLng(position.latitude, position.longitude); + List restaurants = await services.fetchRestaurantsByLocation( + position.latitude, position.longitude); if (restaurants.isNotEmpty) { for (Restaurant thisRestaurant in restaurants) { final MarkerId markerId = MarkerId(thisRestaurant.name); @@ -40,55 +35,59 @@ class MapViewState extends State { markers[markerId] = marker; } } - return markers; + return new MarkersAndLocation(markers: markers, coordinates: location); } @override Widget build(BuildContext context) { return Scaffold( - body: FutureBuilder>( + body: FutureBuilder( future: createMarkers(), - builder: (BuildContext context, - AsyncSnapshot> snapshot) { - Map markers = snapshot.data; - List children; + builder: + (BuildContext context, AsyncSnapshot snapshot) { + MarkersAndLocation data = snapshot.data; + Widget child; if (snapshot.hasData) { final CameraPosition _initialPosition = CameraPosition( - target: LatLng(20, 58), - zoom: 16, + target: data.coordinates, + zoom: 14, + ); + child = GoogleMap( + mapType: MapType.normal, + initialCameraPosition: _initialPosition, + onMapCreated: (GoogleMapController controller) { + _controller.complete(controller); + }, + markers: Set.of(data.markers.values), ); - children = [ - GoogleMap( - mapType: MapType.normal, - initialCameraPosition: _initialPosition, - onMapCreated: (GoogleMapController controller) { - _controller.complete(controller); - }, - markers: Set.of(markers.values), - ), - ]; } else if (snapshot.hasError) { - children = [Text('error')]; - } else { - children = [ - SizedBox( - child: CircularProgressIndicator(), - width: 60, - height: 60, + child = Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [Text("error...")], ), - Padding( - padding: EdgeInsets.only(top: 16), - child: Text('Awaiting result...'), - ) - ]; + ); + } else { + child = Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + SizedBox( + child: CircularProgressIndicator(), + width: 60, + height: 60, + ), + Padding( + padding: EdgeInsets.only(top: 16), + child: Text('Awaiting result...'), + ) + ], + ), + ); } - return Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: children, - ), - ); + return child; }, ), floatingActionButton: Padding( diff --git a/lib/services.dart b/lib/services.dart index c62c336..559100b 100644 --- a/lib/services.dart +++ b/lib/services.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:http/http.dart' as http; import 'package:flutter/foundation.dart'; import './settings.dart'; @@ -452,3 +453,10 @@ class MenuiWorkingHours { MenuiWorkingHours( this.pn, this.wt, this.sr, this.cz, this.pt, this.sb, this.nd); } + +class MarkersAndLocation { + final Map markers; + final LatLng coordinates; + + MarkersAndLocation({@required this.markers, @required this.coordinates}); +}