navigationbaato 0.0.4 copy "navigationbaato: ^0.0.4" to clipboard
navigationbaato: ^0.0.4 copied to clipboard

outdated

A new flutter plugin project.

example/lib/main.dart

import 'package:baato_api/baato_api.dart';
import 'package:baato_api/models/route.dart';
import 'package:flutter/material.dart' hide Route;
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:navigationbaato/library.dart';
import 'package:geolocator/geolocator.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _instruction = "";
  WayPoint _origin = WayPoint(
      name: "Way Point 1",
      latitude: 27.7172,
      longitude: 85.3240);
  final _stop1 = WayPoint(
      name: "Way Point 2",
      latitude: 27.6172,
      longitude: 85.8240);

  Navigationbaato _directions;
  MapBoxOptions _options;
  Route route;

  bool _arrived = false;
  bool _isMultipleStop = false;
  double _distanceRemaining, _durationRemaining;
  MapBoxNavigationViewController _controller;
  bool _routeBuilt = false;
  bool _isNavigating = false;
  static const String BAATO_ACCESS_TOKEN = "bpk.TqpOIK5KflKR_zEI0ONEVdCZmwmfMye9i67nAjsGlMgQ";
  @override
  Future<void> initState() {
    super.initState();
   _determinePosition();
  }
  Future<Position> _determinePosition() async {
    bool serviceEnabled;
    LocationPermission permission;

    // Test if location services are enabled.
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // Location services are not enabled don't continue
      // accessing the position and request users of the
      // App to enable the location services.
      return Future.error('Location services are disabled.');
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        // Permissions are denied, next time you could try
        // requesting permissions again (this is also where
        // Android's shouldShowRequestPermissionRationale
        // returned true. According to Android guidelines
        // your App should show an explanatory UI now.
        return Future.error('Location permissions are denied');
      }
    }

    if (permission == LocationPermission.deniedForever) {
      // Permissions are denied forever, handle appropriately.
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }

    // When we reach here, permissions are granted and we can
    // continue accessing the position of the device.
     var position = await Geolocator.getCurrentPosition();
    this.setState(() {
      _origin = WayPoint(
          name: "Way Point 1",
          latitude: position.latitude,
          longitude: position.longitude);
    });
    initialize(position.latitude, position.longitude);
    List<WayPoint> mWay = [_origin, _stop1];
    _requestRoutingDetails(mWay);
    return position;
  }
  _requestRoutingDetails(List<WayPoint> latLngList) async {
    var points = [];
    for (WayPoint latLng in latLngList)
      points
          .add(latLng.latitude.toString() + "," + latLng.longitude.toString());

    BaatoRoute baatoRoute = BaatoRoute.initialize(
        accessToken: BAATO_ACCESS_TOKEN,
        points: points,
        mode: "car", //can be 'bike', 'car', 'foot'
        alternatives: false, //optional parameter
        instructions: true); //optional parameter

    //get routes between start and destination point
    RouteResponse response = await baatoRoute.getRoutes();
    this.setState(() {
      route = response.data.first;
    });
  }
  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initialize(double latitude, double longitude) async {
    // 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;

    _directions = Navigationbaato(onRouteEvent: _onEmbeddedRouteEvent);
    _options = MapBoxOptions(
      initialLatitude: latitude,
      initialLongitude: longitude,
      zoom: 15.0,
      tilt: 0.0,
      bearing: 0.0,
      enableRefresh: false,
      alternatives: true,
      voiceInstructionsEnabled: true,
      bannerInstructionsEnabled: true,
      allowsUTurnAtWayPoints: true,
      mode: BaatoNavigationMode.car,
      units: VoiceUnits.metric,
      simulateRoute: false,
      animateBuildRoute: true,
      longPressDestinationEnabled: true,
      mapStyleUrl: "https://api.baato.io/api/v1/styles/breeze",
      language: "ne",
      );
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await _directions.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(children: <Widget>[
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    SizedBox(
                      height: 10,
                    ),
                    Text('Running on: $_platformVersion\n'),
                    Container(
                      color: Colors.grey,
                      width: double.infinity,
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: (Text(
                          "Full Screen Navigation",
                          style: TextStyle(color: Colors.white),
                          textAlign: TextAlign.center,
                        )),
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        RaisedButton(
                          child: Text("Start A to B"),
                          onPressed: () async {
                            var wayPoints = List<WayPoint>();
                            wayPoints.add(_origin);
                            wayPoints.add(_stop1);

                            await _directions.startNavigation(
                                wayPoints: wayPoints,
                                options: _options,
                                route: route
                                );
                          },
                        ),
                        // SizedBox(
                        //   width: 10,
                        // ),
                        // RaisedButton(
                        //   child: Text("Start Multi Stop"),
                        //   onPressed: () async {
                        //     _isMultipleStop = true;
                        //     var wayPoints = List<WayPoint>();
                        //     wayPoints.add(_origin);
                        //     wayPoints.add(_stop1);
                        //     wayPoints.add(_origin);
                        //
                        //     await _directions.startNavigation(
                        //         wayPoints: wayPoints,
                        //         options: MapBoxOptions(
                        //             mode: BaatoNavigationMode.car,
                        //             simulateRoute: true,
                        //             language: "en",
                        //             allowsUTurnAtWayPoints: true,
                        //             units: VoiceUnits.metric));
                        //   },
                        // )
                      ],
                    ),
                    // Container(
                    //   color: Colors.grey,
                    //   width: double.infinity,
                    //   child: Padding(
                    //     padding: EdgeInsets.all(10),
                    //     child: (Text(
                    //       "Embedded Navigation",
                    //       style: TextStyle(color: Colors.white),
                    //       textAlign: TextAlign.center,
                    //     )),
                    //   ),
                    // ),
                    // Row(
                    //   mainAxisAlignment: MainAxisAlignment.center,
                    //   children: [
                    //     RaisedButton(
                    //       child: Text(_routeBuilt && !_isNavigating
                    //           ? "Clear Route"
                    //           : "Build Route"),
                    //       onPressed: _isNavigating
                    //           ? null
                    //           : () {
                    //         if (_routeBuilt) {
                    //           _controller.clearRoute();
                    //         } else {
                    //           var wayPoints = List<WayPoint>();
                    //           wayPoints.add(_origin);
                    //           wayPoints.add(_stop1);
                    //           wayPoints.add(_origin);
                    //           _isMultipleStop = wayPoints.length > 2;
                    //           _controller.buildRoute(
                    //               wayPoints: wayPoints);
                    //         }
                    //       },
                    //     ),
                    //     SizedBox(
                    //       width: 10,
                    //     ),
                    //     RaisedButton(
                    //       child: Text("Start "),
                    //       onPressed: _routeBuilt && !_isNavigating
                    //           ? () {
                    //         _controller.startNavigation();
                    //       }
                    //           : null,
                    //     ),
                    //     SizedBox(
                    //       width: 10,
                    //     ),
                    //     RaisedButton(
                    //       child: Text("Cancel "),
                    //       onPressed: _isNavigating
                    //           ? () {
                    //         _controller.finishNavigation();
                    //       }
                    //           : null,
                    //     )
                    //   ],
                    // ),
                    // Center(
                    //   child: Padding(
                    //     padding: EdgeInsets.all(10),
                    //     child: Text(
                    //       "Long-Press Embedded Map to Set Destination",
                    //       textAlign: TextAlign.center,
                    //     ),
                    //   ),
                    // ),
                    // Container(
                    //   color: Colors.grey,
                    //   width: double.infinity,
                    //   child: Padding(
                    //     padding: EdgeInsets.all(10),
                    //     child: (Text(
                    //       _instruction == null || _instruction.isEmpty
                    //           ? "Banner Instruction Here"
                    //           : _instruction,
                    //       style: TextStyle(color: Colors.white),
                    //       textAlign: TextAlign.center,
                    //     )),
                    //   ),
                    // ),
                    // Padding(
                    //   padding: EdgeInsets.only(
                    //       left: 20.0, right: 20, top: 20, bottom: 10),
                    //   child: Column(
                    //     mainAxisAlignment: MainAxisAlignment.center,
                    //     children: <Widget>[
                    //       Row(
                    //         children: <Widget>[
                    //           Text("Duration Remaining: "),
                    //           Text(_durationRemaining != null
                    //               ? "${(_durationRemaining / 60).toStringAsFixed(0)} minutes"
                    //               : "---")
                    //         ],
                    //       ),
                    //       Row(
                    //         children: <Widget>[
                    //           Text("Distance Remaining: "),
                    //           Text(_distanceRemaining != null
                    //               ? "${(_distanceRemaining * 0.000621371).toStringAsFixed(1)} miles"
                    //               : "---")
                    //         ],
                    //       ),
                    //     ],
                    //   ),
                    // ),
                    Divider()
                  ],
                ),
              ),
            ),
            // Expanded(
            //   flex: 1,
            //   child: Container(
            //     height: 1,
            //     width: 1,
            //     color: Colors.grey,
            //     child: MapBoxNavigationView(
            //         options: _options,
            //         onRouteEvent: _onEmbeddedRouteEvent,
            //         onCreated:
            //             (MapBoxNavigationViewController controller) async {
            //           _controller = controller;
            //           controller.initialize();
            //         }),
            //   ),
            // )
          ]),
        ),
      ),
    );
  }

  Future<void> _onEmbeddedRouteEvent(e) async {
    _distanceRemaining = await _directions.distanceRemaining;
    _durationRemaining = await _directions.durationRemaining;

    switch (e.eventType) {
      case MapBoxEvent.progress_change:
        var progressEvent = e.data as RouteProgressEvent;
        _arrived = progressEvent.arrived;
        if (progressEvent.currentStepInstruction != null)
          _instruction = progressEvent.currentStepInstruction;
        break;
      case MapBoxEvent.route_building:
      case MapBoxEvent.route_built:
        setState(() {
          _routeBuilt = true;
        });
        break;
      case MapBoxEvent.route_build_failed:
        setState(() {
          _routeBuilt = false;
        });
        break;
      case MapBoxEvent.navigation_running:
        setState(() {
          _isNavigating = true;
        });
        break;
      case MapBoxEvent.on_arrival:
        _arrived = true;
        if (!_isMultipleStop) {
          await Future.delayed(Duration(seconds: 3));
          await _controller.finishNavigation();
        } else {}
        break;
      case MapBoxEvent.navigation_finished:
      case MapBoxEvent.navigation_cancelled:
        setState(() {
          _routeBuilt = false;
          _isNavigating = false;
        });
        break;
      default:
        break;
    }
    setState(() {});
  }
}
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

A new flutter plugin project.

License

MIT (LICENSE)

Dependencies

baato_api, flutter

More

Packages that depend on navigationbaato