srt_app/lib/pages/map.dart

177 lines
4.5 KiB
Dart
Raw Permalink Normal View History

2024-09-23 18:44:04 +07:00
import 'dart:convert';
2024-09-23 11:52:20 +07:00
import 'package:flutter/material.dart';
2024-09-23 18:44:04 +07:00
import 'package:flutter/services.dart';
2024-09-23 19:20:07 +07:00
import 'package:flutter_map/flutter_map.dart';
2024-10-03 09:00:26 +07:00
import 'package:geojson/geojson.dart';
2024-09-23 18:44:04 +07:00
import 'package:latlong2/latlong.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
}
2024-09-23 11:52:20 +07:00
class MapScreen extends StatefulWidget {
const MapScreen({Key? key}) : super(key: key);
@override
State<MapScreen> createState() => _MapScreenState();
}
2024-09-23 19:20:07 +07:00
class _MapScreenState extends State<MapScreen> {
2024-10-03 09:00:26 +07:00
List<GeoJsonFeature<dynamic>>? stationData;
List<GeoJsonFeature<dynamic>>? linesData;
List<Polyline> _polylines = [];
2024-09-23 19:20:07 +07:00
List<Marker> _markers = [];
2024-09-23 18:44:04 +07:00
@override
void initState() {
2024-10-03 09:00:26 +07:00
WidgetsBinding.instance.addPostFrameCallback((_) {
loadStations().whenComplete(() {
setState(() {
print('Completed');
_loadLines();
});
});
});
2024-09-23 18:44:04 +07:00
2024-10-03 09:00:26 +07:00
super.initState();
2024-09-23 19:20:07 +07:00
}
2024-09-23 18:44:04 +07:00
2024-10-03 09:00:26 +07:00
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);
}
});
}
}
2024-09-23 19:20:07 +07:00
2024-10-03 09:00:26 +07:00
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);
}
});
}
2024-09-23 19:37:32 +07:00
}
2024-10-03 09:00:26 +07:00
void _updateMap(double? zoom) {
if (zoom! >= 10) {
setState(() {
_polylines = [];
_loadMarkers();
});
} else {
_markers = [];
_loadLines();
}
}
/* Future<void> _getMarkers() async {
2024-09-23 19:20:07 +07:00
List<Marker> markers = [];
2024-09-23 18:44:04 +07:00
2024-09-23 19:20:07 +07:00
for (var i = 0; i < _stations.length; i++) {
2024-09-23 18:44:04 +07:00
markers.add(
Marker(
height: 30,
width: 30,
2024-09-23 19:20:07 +07:00
point: LatLng(double.tryParse(_stations[i][4].toString()) ?? 0,
double.tryParse(_stations[i][5].toString()) ?? 0),
2024-09-23 19:37:32 +07:00
builder: (ctx) =>
const Icon(Icons.pin_drop, color: Colors.brown, size: 50.0),
2024-09-23 18:44:04 +07:00
),
);
}
setState(() {
2024-09-23 19:20:07 +07:00
_markers = markers;
2024-09-23 18:44:04 +07:00
});
2024-10-03 09:00:26 +07:00
} */
2024-09-23 18:44:04 +07:00
2024-09-23 11:52:20 +07:00
@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)),
2024-09-23 18:44:04 +07:00
body: FlutterMap(
2024-10-03 09:00:26 +07:00
options: MapOptions(
center: LatLng(13.736717, 100.523186),
zoom: 10.0,
onPositionChanged: (position, hasGesture) {
if (hasGesture) {
_updateMap(position.zoom);
}
},
),
2024-09-23 19:20:07 +07:00
children: [
2024-09-23 18:44:04 +07:00
TileLayer(
2024-10-03 09:00:26 +07:00
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: const <String>[
'a',
'b',
'c',
],
2024-09-23 19:37:32 +07:00
//userAgentPackageName: 'net.winscloud.srt',
2024-09-23 18:44:04 +07:00
),
2024-10-03 09:00:26 +07:00
PolylineLayer(polylines: _polylines),
MarkerLayer(markers: _markers),
2024-09-23 18:44:04 +07:00
],
),
2024-09-23 11:52:20 +07:00
);
}
2024-10-03 09:00:26 +07:00
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;
}
2024-09-23 11:52:20 +07:00
}