flutter_map_heatmap 0.0.4+2 flutter_map_heatmap: ^0.0.4+2 copied to clipboard
A heatmap plugin for flutter_map
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_heatmap/flutter_map_heatmap.dart';
import 'package:latlong2/latlong.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Heatmap Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'flutter_mapmap heat_map demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
StreamController<void> _rebuildStream = StreamController.broadcast();
List<WeightedLatLng> data = [];
List<Map<double, MaterialColor>> gradients = [
HeatMapOptions.defaultGradient,
{0.25: Colors.blue, 0.55: Colors.red, 0.85: Colors.pink, 1.0: Colors.purple}
];
var index = 0;
initState() {
_loadData();
super.initState();
}
@override
dispose() {
_rebuildStream.close();
super.dispose();
}
_loadData() async {
var str = await rootBundle.loadString('assets/initial_data.json');
List<dynamic> result = jsonDecode(str);
setState(() {
data = result
.map((e) => e as List<dynamic>)
.map((e) => WeightedLatLng(LatLng(e[0], e[1]), 1))
.toList();
});
}
void _incrementCounter() {
setState(() {
index = index == 0 ? 1 : 0;
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_rebuildStream.add(null);
});
});
}
@override
Widget build(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_rebuildStream.add(null);
});
final map = new FlutterMap(
options: new MapOptions(center: new LatLng(57.8827, -6.0400), zoom: 8.0),
children: [
TileLayer(
urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: ['a', 'b', 'c']),
if (data.isNotEmpty) HeatMapLayer(
heatMapDataSource: InMemoryHeatMapDataSource(data: data),
heatMapOptions: HeatMapOptions(gradient: this.gradients[this.index],
minOpacity: 0.1),
reset: _rebuildStream.stream,
)
],
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
backgroundColor: Colors.pink,
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Container(child: map),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Switch Gradient',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}