srt_app/lib/pages/map.dart

88 lines
2.2 KiB
Dart
Raw 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-09-23 18:44:04 +07:00
import 'package:flutter_map/plugin_api.dart';
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> {
List _stations = [];
List<Marker> _markers = [];
2024-09-23 18:44:04 +07:00
@override
void initState() {
super.initState();
2024-09-23 19:20:07 +07:00
_getStations();
}
2024-09-23 18:44:04 +07:00
2024-09-23 19:20:07 +07:00
Future<void> _getStations() async {
final String stationFile =
await rootBundle.loadString("assets/data/stations.json");
final data = await json.decode(stationFile);
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 18:44:04 +07:00
builder: (ctx) => const Icon(Icons.pin_drop),
),
);
}
setState(() {
_stations = data["records"];
2024-09-23 19:20:07 +07:00
_markers = markers;
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-09-23 19:20:07 +07:00
options:
MapOptions(center: const LatLng(13.736717, 100.523186), zoom: 2.0),
children: [
2024-09-23 18:44:04 +07:00
TileLayer(
2024-09-23 19:20:07 +07:00
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'net.winscloud,net',
2024-09-23 18:44:04 +07:00
),
2024-09-23 19:20:07 +07:00
MarkerLayer(markers: _markers)
2024-09-23 18:44:04 +07:00
],
),
2024-09-23 11:52:20 +07:00
);
}
}