Skip to content
Snippets Groups Projects
Commit bce69935 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: still no map, but progress

parent 8fdf3f0f
No related branches found
No related tags found
1 merge request!3Choropleth map implementation
......@@ -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),
],
),
),
......
......@@ -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()}');
}
}
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}');
}
}
}
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment