alto_great_circle_distance 0.0.3 alto_great_circle_distance: ^0.0.3 copied to clipboard
Plugin for calculating Great Circle Distance with Spherical Law of Cosines.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:alto_great_circle_distance/alto_great_circle_distance.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
// The distance
double? _distance;
@override
void initState() {
super.initState();
initPlatformState();
_calculate();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await GCD.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _calculate() async {
// The Geolocation of St.Mary Cathedral
final Geo geo1 = Geo(lat: 16.77858116672366, lon: 96.1657684686045);
// The Geolocation of St. Peter's Basilica
final Geo geo2 = Geo(lat: 41.90219864557144, lon: 12.454035933187363);
final double? _theDistance = await GCD().cal(geo1: geo1, geo2: geo2);
setState(() => _distance = _theDistance);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Great Circle Distance'),
),
body: SafeArea(
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(15),
child: Column(
children: [
Text('Running on: $_platformVersion\n'),
Text(
'The distance between St.Mary Cathedral, Myanmar and St.Peter\'s Basilica is ${_distance ?? "-"} km.'),
],
),
),
),
),
);
}
}