Connecting with Geo Smart System

This commit is contained in:
Supan Adit Pratama 2019-10-31 22:51:22 +07:00
parent 093c5b870d
commit 8bec726206
9 changed files with 383 additions and 30 deletions

View File

@ -5,7 +5,8 @@ This is the Mobile Application for [Geo Smart System](https://github.com/supanad
- Flutter
## Todo
- Integration With Geo Smart System
- Integration With Geo Smart System ( OK )
- Documentation
## License
Copyright 2019 Supan Adit Pratama

View File

@ -12,7 +12,8 @@
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:label="geo_app">
android:label="Geo Smart App"
android:usesCleartextTraffic="true">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCjhBV5aq1gsTCYsr6QCCxYusFlBa3DEMs" />

3
lib/config.dart Normal file
View File

@ -0,0 +1,3 @@
class Config {
static const String api = "http://192.168.1.6:8080";
}

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:background_location/background_location.dart';
import 'package:flutter/material.dart';
import 'package:geo_app/model/position.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
@ -55,12 +56,23 @@ class _MyHomePageState extends State<MyHomePage> {
double accuracy = 0.0;
double bearing = 0.0;
double speed = 0.0;
String message = "Connecting to Server";
bool _finish = false;
Position _position;
@override
void initState() {
super.initState();
BackgroundLocation.startLocationService();
message = "Connecting to Server";
Position.getPosition().then((position) {
setState(() {
_position = position;
_finish = this._position.isValid();
});
}, onError: (error) {
message = "Something wrong";
});
}
static final CameraPosition _kGooglePlex = CameraPosition(
@ -71,33 +83,60 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
myLocationEnabled: true,
myLocationButtonEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
BackgroundLocation.getLocationUpdates((location) async {
setState(() {
this.latitude = location.latitude;
this.longitude = location.longitude;
this.accuracy = location.accuracy;
this.altitude = location.altitude;
this.bearing = location.bearing;
this.speed = location.speed;
});
final GoogleMapController controller = await _controller.future;
controller.animateCamera(
CameraUpdate.newCameraPosition(
new CameraPosition(
target: LatLng(this.latitude, this.longitude),
zoom: 19.151926040649414,
body: Stack(
children: <Widget>[
GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
myLocationEnabled: true,
myLocationButtonEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
BackgroundLocation.getLocationUpdates((location) async {
setState(() {
this.latitude = location.latitude;
this.longitude = location.longitude;
this.accuracy = location.accuracy;
this.altitude = location.altitude;
this.bearing = location.bearing;
this.speed = location.speed;
if (this._position != null) {
if (this._position.isValid()) {
this._position.lat = this.latitude.toString();
this._position.lng = this.longitude.toString();
this._position.sendPosition();
}
}
});
// Make Camera Follow the Marker
// final GoogleMapController controller = await _controller.future;
// controller.animateCamera(
// CameraUpdate.newCameraPosition(
// new CameraPosition(
// target: LatLng(this.latitude, this.longitude),
// zoom: 19.151926040649414,
// ),
// ),
// );
});
},
),
Visibility(
visible: !_finish,
child: Container(
decoration: BoxDecoration(
color: Colors.black26.withOpacity(0.8),
),
child: Center(
child: Text(
message,
style: TextStyle(color: Colors.white),
),
),
);
});
},
),
)
],
),
);
}

72
lib/model/position.dart Normal file
View File

@ -0,0 +1,72 @@
import 'dart:convert';
import 'package:geo_app/config.dart';
import 'package:geo_app/model/response.dart';
import 'package:geo_app/model/unique_id.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
class Position {
String id;
String type;
String lat;
String lng;
Position({this.id, this.type, this.lat, this.lng});
Position.fromJson(Map<String, dynamic> json) {
id = json['id'];
type = json['type'];
lat = json['lat'];
lng = json['lng'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['type'] = this.type;
data['lat'] = this.lat;
data['lng'] = this.lng;
return data;
}
static Future<Position> getPosition() async {
SharedPreferences sp = await SharedPreferences.getInstance();
String keyName = "unique_id";
String spId = (sp.getString(keyName) ?? null);
Position position = new Position(
id: spId,
type: "user",
lat: "0.0",
lng: "0.0",
);
if (spId == null) {
final response = await http.get(Config.api + "/unique/id");
if (response.statusCode == 200) {
position.id = UniqueId.fromJson(json.decode(response.body)).id;
} else {
throw Exception("Something Error");
}
sp.setString(keyName, position.id);
}
return position;
}
bool isValid() {
return id != null;
}
Future<Response> sendPosition() async {
Response result;
final response = await http.post(
Config.api + "/set/point",
body: json.encode(this.toJson()),
);
if (response.statusCode == 200) {
result = Response.fromJson(json.decode(response.body));
} else {
throw Exception("Something Error");
}
return result;
}
}

15
lib/model/response.dart Normal file
View File

@ -0,0 +1,15 @@
class Response {
String status;
Response({this.status});
Response.fromJson(Map<String, dynamic> json) {
status = json['status'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
return data;
}
}

15
lib/model/unique_id.dart Normal file
View File

@ -0,0 +1,15 @@
class UniqueId {
String id;
UniqueId({this.id});
UniqueId.fromJson(Map<String, dynamic> json) {
id = json['id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
return data;
}
}

View File

@ -1,6 +1,27 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
adhara_socket_io:
dependency: "direct main"
description:
name: adhara_socket_io
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.38.5"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
@ -22,6 +43,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
build:
dependency: transitive
description:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.1"
build_config:
dependency: transitive
description:
name: build_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1+1"
charcode:
dependency: transitive
description:
@ -29,6 +64,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
checked_yaml:
dependency: transitive
description:
name: checked_yaml
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
collection:
dependency: transitive
description:
@ -36,6 +78,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
cupertino_icons:
dependency: "direct main"
description:
@ -43,6 +106,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2"
dart_style:
dependency: transitive
description:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
flutter:
dependency: "direct main"
description: flutter
@ -53,6 +123,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
front_end:
dependency: transitive
description:
name: front_end
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.27"
glob:
dependency: transitive
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
google_maps_flutter:
dependency: "direct main"
description:
@ -60,6 +144,62 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.21+8"
html:
dependency: transitive
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+3"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.0+2"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.1+1"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
json_serializable:
dependency: "direct main"
description:
name: json_serializable
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.3"
kernel:
dependency: transitive
description:
name: kernel
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.27"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "0.11.3+2"
matcher:
dependency: transitive
description:
@ -74,6 +214,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
node_interop:
dependency: transitive
description:
name: node_interop
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
node_io:
dependency: transitive
description:
name: node_io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1+2"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
path:
dependency: transitive
description:
@ -95,6 +256,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.3.0"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.2"
pubspec_parse:
dependency: transitive
description:
name: pubspec_parse
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
quiver:
dependency: transitive
description:
@ -102,11 +277,25 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.4+3"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.4+6"
source_span:
dependency: transitive
description:
@ -163,6 +352,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
watcher:
dependency: transitive
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+12"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
sdks:
dart: ">=2.2.2 <3.0.0"
flutter: ">=1.5.0 <2.0.0"
dart: ">=2.3.0 <3.0.0"
flutter: ">=1.6.7 <2.0.0"

View File

@ -25,6 +25,10 @@ dependencies:
cupertino_icons: ^0.1.2
google_maps_flutter: ^0.5.21+8
background_location: ^0.0.7
adhara_socket_io: ^0.4.1
http: ^0.12.0+2
json_serializable: ^3.2.3
shared_preferences: ^0.5.4+3
dev_dependencies:
flutter_test: