srt_app/lib/pages/map.dart

88 lines
2.2 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/plugin_api.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 _stations = [];
List<Marker> _markers = [];
@override
void initState() {
super.initState();
_getStations();
}
Future<void> _getStations() async {
final String stationFile =
await rootBundle.loadString("assets/data/stations.json");
final data = await json.decode(stationFile);
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),
),
);
}
setState(() {
_stations = data["records"];
_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: const LatLng(13.736717, 100.523186), zoom: 2.0),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'net.winscloud,net',
),
MarkerLayer(markers: _markers)
],
),
);
}
}