import 'package:flutter_map/flutter_map.dart'; import 'package:geojson/geojson.dart'; import 'package:get/get.dart'; import 'package:latlong2/latlong.dart'; import 'package:rogapp/services/location_service.dart'; import 'package:rogapp/services/perfecture_service.dart'; class IndexController extends GetxController { List locations = [].obs; List currentFeature = [].obs; List perfectures = [].obs; List currentBound = [].obs; List subPerfs = [].obs; MapController? mapController; var mode = 0.obs; String subDropdownValue = "-1"; void toggleMode(){ if(mode==0){ mode += 1; } else{ mode -= 1; } } @override void onInit() { super.onInit(); if(locations.length == 0){ LocationService.loadLocations().then((value){ locations.add(value!); //print(value); }); } if(perfectures.length == 0){ PerfectureService.loadPerfectures().then((value){ perfectures.add(value); loadSubPerfFor("9"); }); } } void loadSubPerfFor(String perf){ subPerfs.clear(); dynamic initVal = {'id':'-1', 'adm2_ja':'----'}; PerfectureService.loadSubPerfectures(perf).then((value){ value!.add(initVal); subPerfs.add(value); subDropdownValue = getSubInitialVal(); }); } String getSubInitialVal(){ int min = 0; if(subPerfs.length > 0){ min = int.parse(subPerfs[0][0]['id'].toString()); for(var sub in subPerfs[0]){ int x = int.parse(sub['id'].toString()); // as int; if(x < min){ min = x; } } } return min.toString(); } void loadLocationforPerf(String perf, MapController mapController) async { locations.clear(); LocationService.loadLocationsFor(perf).then((value){ locations.add(value!); mapController.fitBounds(currentBound[0]); }); } void loadLocationforSubPerf(String subperf, MapController mapController) async { locations.clear(); LocationService.loadLocationsSubFor(subperf).then((value){ locations.add(value!); }); } void setBound(LatLngBounds bounds){ currentBound.clear(); currentBound.add(bounds); } void zoomtoMainPerf(String id){ PerfectureService.getMainPerfExt(id).then((value){ LatLng lat1 = LatLng(value![1], value[0]); LatLng lat2 = LatLng(value[3], value[2]); LatLngBounds bound = LatLngBounds(lat1, lat2); mapController!.fitBounds(bound); setBound(bound); }); } void zoomtoSubPerf(String id){ PerfectureService.getSubExt(id).then((value){ LatLng lat1 = LatLng(value![1], value[0]); LatLng lat2 = LatLng(value[3], value[2]); LatLngBounds bound = LatLngBounds(lat1, lat2); mapController!.fitBounds(bound); setBound(bound); }); } void populateForPerf(String perf, MapController mapController){ loadSubPerfFor(perf); loadLocationforPerf(perf, mapController); zoomtoMainPerf(perf); } void populateForSubPerf(String subperf, MapController mapController){ subDropdownValue = subperf; loadLocationforSubPerf(subperf, mapController); zoomtoSubPerf(subperf); } 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 pt = fs as GeoJsonFeature; 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 pt = fs as GeoJsonFeature; 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); } } } } }