Skip to content

Commit

Permalink
add google maps to application with markers
Browse files Browse the repository at this point in the history
  • Loading branch information
TemKaa1337 committed Mar 25, 2022
1 parent 8ef97e9 commit 1bee8e9
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 16 deletions.
Binary file modified app.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion client/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.temkaatranshprojects.client"
minSdkVersion flutter.minSdkVersion
minSdkVersion 20
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 2 additions & 0 deletions client/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyB27Mxlt4mN5b6phxMpifoeaAOT4mKLlJs"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.temkaatranshprojects.client
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {

}
3 changes: 2 additions & 1 deletion client/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class App extends StatelessWidget {
// 15. add other cities rates (done)
// 16. change app name (done)
// 17. make russian application (done)
// 18. update readme (done_
// 18. update readme (done)
// 19. fix buttons (done)
// 20. add google map (done)

// flutter build apk --release
// build/app/outputs/flutter-apk/app-release.apk
5 changes: 5 additions & 0 deletions client/lib/ui/app_bar/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class DepartmentsAppBar extends StatelessWidget with PreferredSizeWidget {
width: 30,
height: 35,
child: Icon(Icons.settings)
),
SizedBox(
width: 30,
height: 35,
child: Icon(Icons.location_city)
)
],
),
Expand Down
96 changes: 96 additions & 0 deletions client/lib/ui/body/map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';

class GoogleMapTab extends StatefulWidget {
final LatLng userPosition;
final List<Map<String, dynamic>> departmentsInfo;

const GoogleMapTab({
Key? key,
required LatLng this.userPosition,
required List<Map<String, dynamic>> this.departmentsInfo
}) : super(key: key);

@override
_GoogleMapTabState createState() => _GoogleMapTabState();
}

class _GoogleMapTabState extends State<GoogleMapTab>{
late GoogleMapController _controller;

@override
void dispose() {
_controller.dispose();
super.dispose();
}

void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
Set<Marker> markers = {};
CameraPosition position = CameraPosition(
target: widget.userPosition,
zoom: 13
);

Marker userPosition = Marker(
markerId: const MarkerId('user_position'),
infoWindow: const InfoWindow(
title: 'Это вы'
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
position: widget.userPosition
);

markers.add(userPosition);

for(Map<String, dynamic> departmentInfo in widget.departmentsInfo) {
List coordinates = departmentInfo['coordinates'].map((String element) => double.parse(element.trim())).toList();
LatLng departmentPosition = LatLng(coordinates[0], coordinates[1]);

markers.add(Marker(
markerId: const MarkerId('user_position'),
infoWindow: InfoWindow(
title: departmentInfo['name'].toString() + " ${departmentInfo['sign']}${departmentInfo['currency_rate']}",
snippet: " (в ${departmentInfo['distance'].toString()} метрах от вас)"
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
position: departmentPosition
));
}

return Scaffold(
body: Column(
children: [
Expanded(
child: GoogleMap(
initialCameraPosition: position,
onMapCreated: (controller) {
_controller = controller;
},
markers: markers,
zoomControlsEnabled: false,
zoomGesturesEnabled: true,
scrollGesturesEnabled: true,
compassEnabled: true,
rotateGesturesEnabled: true,
mapToolbarEnabled: true,
tiltGesturesEnabled: true,
gestureRecognizers: < Factory < OneSequenceGestureRecognizer >> [
new Factory < OneSequenceGestureRecognizer > (
() => new EagerGestureRecognizer(),
),
].toSet()
)
)
],
)
);
}

}
85 changes: 72 additions & 13 deletions client/lib/ui/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:client/ui/body/department_list.dart';
import 'package:client/services/data/department.dart';
import 'package:client/ui/body/settings.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:geolocator/geolocator.dart';
import 'package:client/ui/errors/department/department_error.dart';
Expand All @@ -13,6 +14,7 @@ import 'package:client/ui/errors/department/department_empty.dart';
import 'package:client/ui/errors/gps/gps_denied.dart';
import 'package:client/ui/errors/gps/gps_disabled.dart';
import 'package:client/ui/errors/gps/gps_waiting.dart';
import 'package:client/ui/body/map.dart';
import 'dart:convert';

class Home extends StatefulWidget {
Expand All @@ -23,7 +25,8 @@ class Home extends StatefulWidget {
}

class _HomeState extends State<Home> with TickerProviderStateMixin {
late final TabController _tabController = TabController(length: 2, vsync: this);
late final TabController _tabController = TabController(length: 3, vsync: this);
bool _refreshButtonVisible = true;

DepartmentState _requestState = DepartmentState.loading;
List<Department> _departments = [];
Expand Down Expand Up @@ -117,14 +120,26 @@ class _HomeState extends State<Home> with TickerProviderStateMixin {
super.initState();
_tabController.addListener(() {
if (!_tabController.indexIsChanging) {
setState(() {

});
if ([0, 1].contains(_tabController.index)) {
setState(() {
_refreshButtonVisible = true;
});
} else {
setState(() {
_refreshButtonVisible = false;
});
}
}
});
getNearestDepartments();
}

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

void refresh(String currency, String operation, int radius, int departmentNumber) {
_currency = currency;
_operation = operation;
Expand All @@ -134,10 +149,12 @@ class _HomeState extends State<Home> with TickerProviderStateMixin {
_tabController.animateTo(0);
_tabController.index = 0;

_refreshButtonVisible = true;

getNearestDepartments();
}

Widget getCurrentWidget() {
Widget getCurrentDepartmentWidget() {
if (_gpsState == GpsState.gpsDisabled) {
return const GpsDisabled();
} else if (_gpsState == GpsState.waitingForPermission) {
Expand Down Expand Up @@ -170,6 +187,44 @@ class _HomeState extends State<Home> with TickerProviderStateMixin {
return const Text('');
}

Widget getCurrentMapWidget() {
if (_gpsState == GpsState.gpsDisabled) {
return const GpsDisabled();
} else if (_gpsState == GpsState.waitingForPermission) {
return const GpsWaiting();
} else if (_gpsState == GpsState.permissionDenied) {
return const GpsDenied();
} else {
if (_requestState == DepartmentState.loading) {
return const DepartmentLoading();
} else if (_requestState == DepartmentState.success) {
LatLng userPosition = LatLng(_position.latitude, _position.longitude);
// LatLng userPosition = LatLng(53.896171, 27.543516);

List<Map<String, dynamic>> departmentsInfo = _departments.map((Department department) {
return {
'coordinates': department.coordinates,
'name': department.bankName,
'distance': department.distance,
'currency_rate': department.currencyRates[_currency.toLowerCase()]![_operation == 'Buy' ? 'bank_sells' : 'bank_buys' ]!,
'sign': _currency == 'USD' ? '\$' : '€'
};
}).toList();

return GoogleMapTab(
userPosition: userPosition,
departmentsInfo: departmentsInfo
);
} else if (_requestState == DepartmentState.error) {
return const DepartmentError();
} else if (_requestState == DepartmentState.empty) {
return const DepartmentEmpty();
}
}

return Text('');
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -178,16 +233,20 @@ class _HomeState extends State<Home> with TickerProviderStateMixin {
controller: _tabController,
physics: const BouncingScrollPhysics(),
children: [
getCurrentWidget(),
Settings(refresh: refresh)
getCurrentDepartmentWidget(),
Settings(refresh: refresh),
getCurrentMapWidget()
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
refresh(_currency, _operation, _radius, _departmentNumber);
},
child: const Icon(Icons.refresh)
),
floatingActionButton: Visibility(
visible: _refreshButtonVisible,
child: FloatingActionButton(
onPressed: () {
refresh(_currency, _operation, _radius, _departmentNumber);
},
child: const Icon(Icons.refresh)
),
)
);
}
}
28 changes: 28 additions & 0 deletions client/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -149,6 +156,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
google_maps_flutter:
dependency: "direct main"
description:
name: google_maps_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
google_maps_flutter_platform_interface:
dependency: transitive
description:
name: google_maps_flutter_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
http:
dependency: "direct main"
description:
Expand Down Expand Up @@ -252,6 +273,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies:
sdk: flutter
http: 0.13.4
geolocator: ^8.2.0

google_maps_flutter: ^2.1.3

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit 1bee8e9

Please sign in to comment.