Skip to content

Instantly share code, notes, and snippets.

@osule
Last active August 6, 2025 12:12
Show Gist options
  • Save osule/eceaf7cf69f8c7dd2e305986eecb43a0 to your computer and use it in GitHub Desktop.
Save osule/eceaf7cf69f8c7dd2e305986eecb43a0 to your computer and use it in GitHub Desktop.
flutter_map_geojson2 attribute filtering example
import 'package:flutter/material.dart';
import 'package:flutter_map_geojson2/flutter_map_geojson2.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
// @override
Widget build(BuildContext context) {
return MaterialApp(
// Application name
title: 'Flutter GeoJSON Demo',
// Application theme data, you can set the colors for the application as
// you want
theme: ThemeData(
// useMaterial3: false,
primarySwatch: Colors.blue,
),
// A widget which will be started on application startup
home: PolygonFeatureCollection(title: 'Polygons Feature Collection'),
);
}
}
class PolygonFeatureCollection extends StatelessWidget {
final String title;
const PolygonFeatureCollection({super.key, required this.title});
@override
Widget build(BuildContext context) {
GeoJsonStyleDefaults styleDefaults = GeoJsonStyleDefaults(
strokeColor: Colors.red,
strokeOpacity: 1,
strokeWidth: 3,
fillColor: Colors.blue,
);
return Scaffold(
appBar: AppBar(
// The title text which will be shown on the action bar
title: Text(title),
),
body: Center(
child: FlutterMap(
options: const MapOptions(
initialCenter: LatLng(10.648822941560567, 13.269154369251979),
initialZoom: 4.0,
),
children: [
GeoJsonLayer.asset(
'assets/data/sample.geojson',
onPolygon: (List<LatLng> points, List<List<LatLng>>? holes,
Map<String, dynamic> props,
{GeoJsonStyleDefaults? defaults}) {
defaults ??= styleDefaults;
final String colorHex = props['my_color'] ?? '';
Color fillColor = defaults.fillColor;
try {
fillColor =
Color(int.parse("0xFF${colorHex.replaceAll('#', '')}"));
} catch (e) {
debugPrint("Invalid color: $colorHex");
}
return Polygon(
points: points,
holePointsList: holes,
color: fillColor,
borderColor: defaults.strokeColor,
borderStrokeWidth: 2.0);
},
),
],
),
),
);
}
}
name: fluttergeojsondemo
description: Flutter GeoJSON Demo.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ^3.0.0
dependencies:
flutter:
sdk: flutter
flutter_map_geojson2: ^1.0.2
flutter_map: ^7.0.2
latlong2: ^0.9.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: true
assets:
- assets/data/sample.geojson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment