google_distance_matrix 0.0.1 copy "google_distance_matrix: ^0.0.1" to clipboard
google_distance_matrix: ^0.0.1 copied to clipboard

Google distance matrix

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:google_distance_matrix/google_distance_matrix.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GoogleDistanceMatrix googleDistanceMatrix = GoogleDistanceMatrix();

  int distance = 0;

  void _getDistanceMatrix() async {
    var distanceMatrix = await googleDistanceMatrix.getDistance(
      'GOOGLE_MAP_API_KEY',
      origin: Coordinate(latitude: '-7.7665339', longitude: '110.3333601'),
      destination: Coordinate(latitude: '-7.7602694', longitude: '110.4051345'),
    );

    setState(() {
      distance = distanceMatrix;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('Origin: -7.7665339, 110.3333601'),
            const Text('destination: -7.7602694,110.4051345'),
            Text('$distance'),
            ElevatedButton(
              onPressed: _getDistanceMatrix,
              child: const Text('Get Distance'),
            )
          ],
        ),
      ),
    );
  }
}