ubhub_maps_flutter 0.1.0
ubhub_maps_flutter: ^0.1.0 copied to clipboard
Flutter map and place-search widgets for the UBHUB public geospatial API.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:ubhub_maps_flutter/ubhub_maps_flutter.dart';
// Pass a restricted key with geocode + basemap scopes using --dart-define.
// Client-distributed keys are extractable; restrict and rate-limit them.
void main() => runApp(const MaterialApp(home: MapExample()));
/// A map and place search sharing one public UBHUB API key.
class MapExample extends StatefulWidget {
/// Creates the example application.
const MapExample({super.key});
@override
State<MapExample> createState() => _MapExampleState();
}
class _MapExampleState extends State<MapExample> {
static const _key = String.fromEnvironment('UBHUB_API_KEY');
late final UbHubGeo _client;
late final UbHubMapOptions _options;
UbHubMapController? _map;
List<GeoPoint> _markers = const [];
@override
void initState() {
super.initState();
if (_key.isNotEmpty) {
_client = UbHubGeo(apiKey: _key);
_options = UbHubMapOptions(apiKey: _key);
}
}
@override
void dispose() {
if (_key.isNotEmpty) _client.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_key.isEmpty) {
return const Scaffold(
body: Center(
child: Text(
'Set UBHUB_API_KEY with --dart-define to run this example.',
),
),
);
}
return Scaffold(
appBar: AppBar(title: const Text('UBHUB Maps')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
child: UbHubPlaceSearch(
client: _client,
onSelected: (place) async {
setState(() => _markers = [place.location]);
await _map?.flyTo(place.location);
},
),
),
Expanded(
child: UbHubMap(
options: _options,
markers: _markers,
onReady: (controller) => _map = controller,
),
),
],
),
);
}
}