Flutter app can not working with google maps

33 Views Asked by At

E/flutter (23677): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Unable to get route: Response ---> REQUEST_DENIED E/flutter (23677): #0 NetworkUtil.getRouteBetweenCoordinates (package:flutter_polyline_points/src/network_util.dart:39:9) E/flutter (23677): E/flutter (23677): #1 PolylinePoints.getRouteBetweenCoordinates (package:flutter_polyline_points/flutter_polyline_points.dart:32:20) E/flutter (23677): E/flutter (23677): #2 OrderTrackingPageState.getPolyPoints (package:untitled8/news/screen.dart:33:29) E/flutter (23677): E/flutter (23677):

import 'dart:async';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
import 'constants.dart';

class OrderTrackingPage extends StatefulWidget {
  const OrderTrackingPage({Key? key}) : super(key: key);

  @override
  State<OrderTrackingPage> createState() => OrderTrackingPageState();
}

class OrderTrackingPageState extends State<OrderTrackingPage> {
  final Completer<GoogleMapController> _controller = Completer();

  static const LatLng sourceLocation = LatLng(37.33500926, -122.03272188);
  static const LatLng destination = LatLng(37.33429383, -122.06600055);
  List<LatLng> polylineCoordinates = [];
  LocationData? currentLocation;

  void getCurrentLocaiton() {
    Location location = Location();
    location.getLocation().then((location) {
      currentLocation = location;
    });
  }

  Future<void> getPolyPoints() async {
    PolylinePoints polylinePoints = PolylinePoints();
    PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
      google_api_key,
      PointLatLng(sourceLocation.latitude, sourceLocation.longitude),
      PointLatLng(destination.latitude, destination.longitude),
    );
    if (result.points.isNotEmpty) {
      result.points.forEach((PointLatLng point) {
        polylineCoordinates.add(LatLng(point.latitude, point.longitude));
      });
      setState(() {});
    }
  }

  @override
  void initState() {
    getCurrentLocaiton();
    getPolyPoints();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Track Order',
          style: TextStyle(color: Colors.black, fontSize: 16),
        ),
      ),
      body: currentLocation == null
          ? const Center(child: Text("Loading"))
          : GoogleMap(
              initialCameraPosition:
                  CameraPosition(target: LatLng(currentLocation!.latitude!, currentLocation!.longitude!), zoom: 13.5),
              onMapCreated: (GoogleMapController controller) {
                _controller.complete(controller);
              },
              polylines: {
                Polyline(
                  polylineId: PolylineId("route"),
                  points: polylineCoordinates,
                  color: primaryColor, // Set the color of the polyline
                  width: 6, // Set the width of the polyline
                ),
              },
              markers: {
                Marker(
                  markerId: const MarkerId("currentLocati"),
                  position: LatLng(currentLocation!.latitude!, currentLocation!.longitude!),
                ),
                const Marker(
                  markerId: MarkerId("source"),
                  position: sourceLocation,
                ),
                const Marker(
                  markerId: MarkerId("destination"),
                  position: destination,
                ),
              },
            ),
    );
  }
}

I tried the video on youtube but app is not working also i can not see the route.

0

There are 0 best solutions below