map_services 0.0.5
map_services: ^0.0.5 copied to clipboard
A Flutter package for mapping services including distance calculation, reverse geocoding, and routing.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:map_services/map_services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Map Services Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final MapServices _mapServices = MapServices();
@override
void initState() {
super.initState();
}
final _searchController = TextEditingController();
String _distanceResult = '';
List<CoordinatedLocationResult> _searchResults = [];
bool _isLoading = false;
void _calculateDistance() {
final manila = LatLong(14.5995, 120.9842);
final makati = LatLong(14.5547, 121.0244);
final distance = _mapServices.getDistanceInKm(
pointA: manila,
pointB: makati,
);
setState(() {
_distanceResult =
'Distance from Manila to Makati: ${distance.toStringAsFixed(2)} km';
});
}
Future<void> _searchAddress() async {
if (_searchController.text.isEmpty) return;
setState(() {
_isLoading = true;
_searchResults = [];
});
try {
final results = await MapServices.searchAddress(_searchController.text);
setState(() {
_searchResults = results;
});
} catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: $e')));
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Map Services Example'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
const Text(
'Distance Calculation',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _calculateDistance,
child: const Text('Calculate Manila to Makati'),
),
if (_distanceResult.isNotEmpty) ...[
const SizedBox(height: 8),
Text(_distanceResult),
],
],
),
),
),
const SizedBox(height: 16),
const Text(
'Address Search',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: TextField(
controller: _searchController,
decoration: const InputDecoration(
hintText: 'Enter address (e.g. Manila)',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 8),
IconButton(
onPressed: _isLoading ? null : _searchAddress,
icon: const Icon(Icons.search),
),
],
),
const SizedBox(height: 16),
if (_isLoading)
const Center(child: CircularProgressIndicator())
else
Expanded(
child: ListView.builder(
itemCount: _searchResults.length,
itemBuilder: (context, index) {
final item = _searchResults[index];
return ListTile(
title: Text(item.address ?? 'No address'),
subtitle: Text(
'${item.coordinates.latitude}, ${item.coordinates.longitude}',
),
leading: const Icon(Icons.location_on),
);
},
),
),
],
),
),
);
}
}