srt_app/lib/pages/map.dart

177 lines
4.5 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:geojson/geojson.dart';
import 'package:latlong2/latlong.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
}
class MapScreen extends StatefulWidget {
const MapScreen({Key? key}) : super(key: key);
@override
State<MapScreen> createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
List<GeoJsonFeature<dynamic>>? stationData;
List<GeoJsonFeature<dynamic>>? linesData;
List<Polyline> _polylines = [];
List<Marker> _markers = [];
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
loadStations().whenComplete(() {
setState(() {
print('Completed');
_loadLines();
});
});
});
super.initState();
}
void _loadMarkers() {
if (stationData != null) {
stationData!.forEach((data) {
if (data.type == GeoJsonFeatureType.point) {
GeoJsonPoint point = data.geometry;
final marker = Marker(
point: point.geoPoint.toLatLng()!,
height: 30,
width: 30,
builder: (ctx) => Tooltip(
message: data.properties?['name'] ?? '',
triggerMode: TooltipTriggerMode.tap,
child: const Icon(Icons.circle,
color: Colors.deepPurple, size: 20.0),
));
_markers.add(marker);
}
});
}
}
void _loadLines() {
if (linesData != null) {
linesData!.forEach((data) {
if (data.type == GeoJsonFeatureType.line) {
GeoJsonLine line = data.geometry;
final poly = Polyline(
points: line.geoSerie!.toLatLng(),
color: Colors.blue,
strokeWidth: 5);
_polylines.add(poly);
}
});
}
}
void _updateMap(double? zoom) {
if (zoom! >= 10) {
setState(() {
_polylines = [];
_loadMarkers();
});
} else {
_markers = [];
_loadLines();
}
}
/* Future<void> _getMarkers() async {
List<Marker> markers = [];
for (var i = 0; i < _stations.length; i++) {
markers.add(
Marker(
height: 30,
width: 30,
point: LatLng(double.tryParse(_stations[i][4].toString()) ?? 0,
double.tryParse(_stations[i][5].toString()) ?? 0),
builder: (ctx) =>
const Icon(Icons.pin_drop, color: Colors.brown, size: 50.0),
),
);
}
setState(() {
_markers = markers;
});
} */
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.more_vert),
onPressed: () {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('More')));
},
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => {print("deez")}, child: const Icon(Icons.add)),
body: FlutterMap(
options: MapOptions(
center: LatLng(13.736717, 100.523186),
zoom: 10.0,
onPositionChanged: (position, hasGesture) {
if (hasGesture) {
_updateMap(position.zoom);
}
},
),
children: [
TileLayer(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: const <String>[
'a',
'b',
'c',
],
//userAgentPackageName: 'net.winscloud.srt',
),
PolylineLayer(polylines: _polylines),
MarkerLayer(markers: _markers),
],
),
);
}
Future<List<GeoJsonFeature>?> loadStations() async {
try {
final String stationFile =
await rootBundle.loadString("assets/data/stations.geojson");
final String linesFile =
await rootBundle.loadString("assets/data/lines.geojson");
final rawStation = GeoJson();
await rawStation.parse(stationFile);
stationData = rawStation.features;
final rawLines = GeoJson();
await rawLines.parse(linesFile);
linesData = rawLines.features;
// final data = await json.decode(stationFile);
} catch (e) {
print(e.toString());
rethrow;
}
return stationData;
}
}