From bce699351507713d3b8590d3187c06f373a56d59 Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Tue, 27 Feb 2024 12:30:14 +0100 Subject: [PATCH] update: still no map, but progress --- app/lib/pages/default_page.dart | 5 +- app/lib/pages/marker_handler/get_markers.dart | 31 +++++++++++ app/lib/pages/widgets/cloropleth_map.dart | 54 ++++++++----------- app/lib/pages/widgets/map_widget.dart | 8 ++- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart index 29ad2d91..728f8238 100644 --- a/app/lib/pages/default_page.dart +++ b/app/lib/pages/default_page.dart @@ -17,14 +17,17 @@ class _DefaultPageState extends State<DefaultPage> { bool showBar = false; List<Measurement> markerList = []; + String relation = ''; // Call fetchMarkerTemplate and await its result before setting the state Future<void> loadMarkerList() async { try { List<Measurement> fetchedMarkers = await fetchMarkerData(); + String fetchedRelation = await fetchRelation(); setState(() { markerList = fetchedMarkers; + relation = fetchedRelation; }); } catch (e) { showDialog( @@ -95,7 +98,7 @@ class _DefaultPageState extends State<DefaultPage> { ), body: ListView( children: [ // Add main widget - MapContainerWidget(markerList: markerList), + MapContainerWidget(markerList: markerList, relation: relation), ], ), ), diff --git a/app/lib/pages/marker_handler/get_markers.dart b/app/lib/pages/marker_handler/get_markers.dart index 156de62c..2d5c4ee4 100644 --- a/app/lib/pages/marker_handler/get_markers.dart +++ b/app/lib/pages/marker_handler/get_markers.dart @@ -42,3 +42,34 @@ Future<List<Measurement>> fetchMarkerData() async { } } +// fetchRelation requests lake relation data from the server +Future<String> fetchRelation() async { + try { + // Custom HTTP client + HttpClient client = HttpClient() + ..badCertificateCallback = // NB: temporary disable SSL certificate validation + (X509Certificate cert, String host, int port) => true; + + // Request markers from server + var request = await client.getUrl(Uri.parse(serverURI + 'get_relation')); + var response = await request.close(); // Close response body at end of function + + // Parse body to JSON if request is ok + if (response.statusCode == 200) { + var responseBody = await response.transform(utf8.decoder).join(); + + if (responseBody.isNotEmpty) { + return responseBody; + } else { + throw Exception('Failed to fetch relation, body is empty'); + } + } else { + throw Exception('Failed to fetch relation: Status code ${response.statusCode}'); + } + } catch (e) { + throw Exception('Failed to fetch relation: ${e.toString()}'); + } +} + + + diff --git a/app/lib/pages/widgets/cloropleth_map.dart b/app/lib/pages/widgets/cloropleth_map.dart index 404de178..f5ec9be3 100644 --- a/app/lib/pages/widgets/cloropleth_map.dart +++ b/app/lib/pages/widgets/cloropleth_map.dart @@ -1,36 +1,38 @@ import 'dart:convert'; import 'package:flutter/material.dart'; -import 'package:http/http.dart' as http; import 'package:syncfusion_flutter_maps/maps.dart'; - class ChoroplethMap extends StatefulWidget { + final String relation; + + const ChoroplethMap({Key? key, + required this.relation, + }) : super(key: key); + @override _ChoroplethMapState createState() => _ChoroplethMapState(); } -class _ChoroplethMapState extends State { +class _ChoroplethMapState extends State<ChoroplethMap> { late MapShapeSource mapShapeSource; @override void initState() { super.initState(); - getRelation().then((geojsonData) { - setState(() { - final geoJsonData = json.decode(geojsonData); - - mapShapeSource = MapShapeSource.asset( - geoJsonData, - shapeDataField: 'coordinates', - dataCount: geoJsonData.length, - primaryValueMapper: (int index) { - return 'lake'; // NB: temp hardcoded value, no subdivisions yet - }, - ); - }); - }).catchError((error) { - throw Exception('Failed to retrieve geojson data'); + final geoJsonData = json.decode(widget.relation); // Decode the JSON string + final coordinates = geoJsonData['features'][0]['geometry']['coordinates'][0]; + + setState(() { + mapShapeSource = MapShapeSource.asset( + widget.relation, + shapeDataField: 'coordinates', + dataCount: coordinates.length, + primaryValueMapper: (int index) { + return 'lake'; // NB: temp hardcoded value, no subdivisions yet + }, + ); }); + } @override @@ -40,23 +42,9 @@ class _ChoroplethMapState extends State { MapShapeLayer( source: mapShapeSource, strokeColor: Colors.white30, - legend: const MapLegend.bar( - MapElement.shape, - position: MapLegendPosition.bottom, - segmentSize: Size(55.0, 9.0), - ), ), ], ); } - - // getRelation requests the polygon coordinates from the server - Future<String> getRelation() async { - final response = await http.get(Uri.parse('get_relation')); - if (response.statusCode == 200) { - return response.body; - } else { - throw Exception('Failed to retrieve geojson data from server. Status code: ${response.statusCode}'); - } - } } + diff --git a/app/lib/pages/widgets/map_widget.dart b/app/lib/pages/widgets/map_widget.dart index 694b5dc9..6508fbfc 100644 --- a/app/lib/pages/widgets/map_widget.dart +++ b/app/lib/pages/widgets/map_widget.dart @@ -14,8 +14,12 @@ import 'package:latlong2/latlong.dart' as latLng; /// its layers, polygons and markers. class MapContainerWidget extends StatefulWidget { final List<Measurement> markerList; + final String relation; - const MapContainerWidget({Key? key, required this.markerList}) : super(key: key); + const MapContainerWidget({Key? key, + required this.markerList, + required this.relation, + }) : super(key: key); @override _MapContainerWidgetState createState() => _MapContainerWidgetState(); @@ -60,7 +64,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { SizedBox( width: screenWidth * boxWidth, height: screenWidth * boxHeight, - child: ChoroplethMap() + child: ChoroplethMap(relation: widget.relation) /*FlutterMap( options: MapOptions( center: mapCenter, -- GitLab