srt_app/lib/pages/map.dart

95 lines
2.3 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.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);
setState(() {
_stations = data["records"];
_getMarkers();
});
}
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: const LatLng(13.736717, 100.523186), zoom: 10.0),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
//userAgentPackageName: 'net.winscloud.srt',
),
MarkerLayer(markers: _markers)
],
),
);
}
}