0

I need help with google_directions_api in flutter and I tyr to create an request for get an diretions. I want to create an request for get data about my route like in google maps with step by step navigation (turn right 50m or something like this). I need help with ceating an correct request for get data

Here is one of requests which i search

Future<void> fetchCustomDirections(List<LatLng> customCoordinates) async {
    final directionsService = DirectionsService();

    final waypoints = customCoordinates
        .sublist(1, customCoordinates.length - 1)
        .map((coordinate) => DirectionsWaypoint(
              location: LatLng(coordinate.latitude, coordinate.longitude),
            ))
        .toList();

    final request = DirectionsRequest(
      origin: LatLng(
        customCoordinates.first.latitude,
        customCoordinates.first.longitude,
      ),
      destination: LatLng(
        customCoordinates.last.latitude,
        customCoordinates.last.longitude,
      ),
      alternatives: false,
      language: 'en',
      waypoints: waypoints,
    );

    final response = await directionsService.route(request, (
      DirectionsResult? response,
      DirectionsStatus? status,
    ) {
      print('Status of request: $status');

      if (status == DirectionsStatus.ok) {
        final steps = response!.routes!.first.legs!.first.steps!;
        print('Steps: $steps');
        // Update your UI with the response data
      } else {
        print('Error fetching data about route:');
      }
    });
  }

Here is my request which I want to use for my case

Future<void> fetchCustomDirections(List<LatLng> customCoordinates) async {
    final directionsService = DirectionsService();

    final waypoints = customCoordinates
        .sublist(1, customCoordinates.length - 1)
        .map((coordinate) => DirectionsWaypoint(
              location: '${coordinate.latitude},${coordinate.longitude}',
            ))
        .toList();

    final request = DirectionsRequest(
      origin: '${customCoordinates.first.latitude},${customCoordinates.first.longitude}',
      destination: '${customCoordinates.last.latitude},${customCoordinates.last.longitude}',
      alternatives: false,
      language: 'en',
      waypoints: waypoints,
    );

    final response = await directionsService.route(request, (
      DirectionsResult? response,
      DirectionsStatus? status,
    ) {
      print('Statue of request: $status');

      if (status == DirectionsStatus.ok) {
        final steps = response!.routes!.first.legs!.first.steps!;
        print('Steps: $steps');
        setState(() {
          responseData = steps.toString();
        });
      } else {
        print('Error fetching data about route:');
      }
    });
  }

And here is my request for get an directions from api

I/flutter ( 6397): Statue of request: REQUEST_DENIED

AndrewS19
  • 23
  • 5
  • According to Google Maps Platform [Response Code Graph](https://developers.google.com/maps/reporting-and-monitoring/reporting#response-code-graphs), `REQUEST_DENIED` is usually a client error caused by authentication error, access error, etc. So you better check if Directions API is enabled, you have set the proper restrictions, and etc. But your best bet here is to contact support so that they can take a look about the error internally and help you with your issue. https://developers.google.com/maps/support#contact-maps-support – Yrll Jun 20 '23 at 03:36

1 Answers1

0

You must use an API_KEY for your request to work

Please see documentation for guidance in how to set-up your project and API_KEY using Directions API.

As per your given sample code, the error you have encountered is REQUEST_DENIED which is usually a client error caused by authentication error, access error, etc.

And looking at your code, you did not have DirectionsService.init('API_KEY'); at the beginning of your code block or before your final directionsService = DirectionsService();.

Doing a quick search on the google_directions_api flutter library documentation/repository will give you this sample code that shows that you need to initialize the DirectionsService with an API_KEY first like so:

import 'package:google_directions_api/google_directions_api.dart';

void main() {
  // You did not have this in your sample code
  DirectionsService.init('API_KEY');

  final directionsService = DirectionsService();

  final request = DirectionsRequest(
    origin: 'New York',
    destination: 'San Francisco',
    travelMode: TravelMode.driving,
  );

  directionsService.route(request,
      (DirectionsResult response, DirectionsStatus status) {
    if (status == DirectionsStatus.ok) {
      // do something with successful response
    } else {
      // do something with error response
    }
  });
}

I hope this helps!

Yrll
  • 1,619
  • 2
  • 5
  • 19