Files
rog_app/lib/pages/home/home_controller.dart
Mohamed Nouffer ee3845681d update
2022-03-30 16:19:18 +05:30

155 lines
4.8 KiB
Dart

import 'package:flutter_map/plugin_api.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/pages/map/map_page.dart';
import 'package:rogapp/services/location_service.dart';
import 'package:rogapp/services/perfecture_service.dart';
class HomeController extends GetxController {
List<GeoJsonFeatureCollection> locations = <GeoJsonFeatureCollection>[].obs;
List<GeoJsonFeature> currentFeature = <GeoJsonFeature>[].obs;
List<dynamic> perfectures = <dynamic>[].obs;
List<LatLngBounds> currentBound = <LatLngBounds>[].obs;
List<dynamic> subPerfs = <dynamic>[].obs;
@override
void onInit() {
LocationService.loadLocations().then((value){
locations.add(value!);
});
PerfectureService.loadPerfectures().then((value){
perfectures.add(value);
});
super.onInit();
}
void getBoundFromLatLng(List<LatLng> list) {
double? x0, x1, y0, y1;
for (LatLng latLng in list) {
if (x0 == null) {
x0 = x1 = latLng.latitude;
y0 = y1 = latLng.longitude;
} else {
if (latLng.latitude > x1!) x1 = latLng.latitude;
if (latLng.latitude < x0) x0 = latLng.latitude;
if (latLng.longitude > y1!) y1 = latLng.longitude;
if (latLng.longitude < y0!) y0 = latLng.longitude;
}
}
currentBound.clear();
currentBound.add(LatLngBounds(LatLng(x1!, y1!), LatLng(x0!, y0!)));
}
void setBounds(){
List<LatLng> lts = [];
if(locations.length > 0){
for(GeoJsonFeature i in locations[0].collection){
GeoJsonMultiPoint p = i.geometry as GeoJsonMultiPoint;
LatLng lt = LatLng(p.geoSerie!.geoPoints[0].latitude , p.geoSerie!.geoPoints[0].longitude) ;
lts.add(lt);
}
}
else{
LatLng lt = LatLng(37.15319600454702, 139.58765950528198);
lts.add(lt);
}
getBoundFromLatLng(lts);
}
void loadLocationforPerf(String perf){
locations.clear();
LocationService.loadLocationsFor(perf).then((value){
locations.add(value!);
setBounds();
MapPage.mapController.fitBounds(currentBound[0]);
});
}
String getSubInitialVal(){
int min = 0;
if(subPerfs.length > 0){
min = subPerfs[0][0]['id'] as int;
for(var sub in subPerfs[0]){
int x = sub['id'] as int;
if(x < min){
min = x;
}
}
}
return min.toString();
}
void loadSubPerfFor(String perf){
subPerfs.clear();
PerfectureService.loadSubPerfectures(perf).then((value){
subPerfs.add(value);
print(subPerfs);
});
}
GeoJsonFeature? getFeatureForLatLong(double lat, double long){
if(locations.length > 0){
for(GeoJsonFeature i in locations[0].collection){
GeoJsonMultiPoint p = i.geometry as GeoJsonMultiPoint;
if(p.geoSerie!.geoPoints[0].latitude == lat && p.geoSerie!.geoPoints[0].longitude == long){
return i;
}
}
}
}
void makeNext(GeoJsonFeature fs){
GeoJsonFeature<GeoJsonMultiPoint> pt = fs as GeoJsonFeature<GeoJsonMultiPoint>;
for(int i=0; i<= locations[0].collection.length - 1; i++){
GeoJsonMultiPoint p = locations[0].collection[i].geometry as GeoJsonMultiPoint;
if(p.geoSerie!.geoPoints[0].latitude == pt.geometry!.geoSerie!.geoPoints[0].latitude && p.geoSerie!.geoPoints[0].longitude == pt.geometry!.geoSerie!.geoPoints[0].longitude ){
if(currentFeature.length > 0){
currentFeature.clear();
}
if(i >= locations[0].collection.length - 1 ){
currentFeature.add(locations[0].collection[0] as GeoJsonFeature);
}
else{
currentFeature.add(locations[0].collection[i + 1] as GeoJsonFeature);
}
}
}
}
void makePrevious(GeoJsonFeature fs){
GeoJsonFeature<GeoJsonMultiPoint> pt = fs as GeoJsonFeature<GeoJsonMultiPoint>;
for(int i=0; i<= locations[0].collection.length - 1; i++){
GeoJsonMultiPoint p = locations[0].collection[i].geometry as GeoJsonMultiPoint;
if(p.geoSerie!.geoPoints[0].latitude == pt.geometry!.geoSerie!.geoPoints[0].latitude && p.geoSerie!.geoPoints[0].longitude == pt.geometry!.geoSerie!.geoPoints[0].longitude ){
if(currentFeature.length > 0){
currentFeature.clear();
}
if(i == 0 ){
currentFeature.add(locations[0].collection[locations[0].collection.length -1] as GeoJsonFeature);
}
else{
currentFeature.add(locations[0].collection[i - 1] as GeoJsonFeature);
}
}
}
}
}