Flutter polyline issue

Issue

I am currently working for google map. I follow all the steps at GCP and also enable direction API for google map.Now I want to show polyline at map between source and destination. But as I run an application on real device it can’t shown. I get only markers of source and destination.
Please see my code at below.

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

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

double _originLatitude = 6.5212402;
double _originLongitude = 3.3679965;
double _destLatitude = 6.849660;
double _destLongitude = 3.648190;
Map<MarkerId, Marker> markers = {};

PolylinePoints polylinePoints = PolylinePoints();
Map<PolylineId, Polyline> polylines = {};

class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();

static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(_originLatitude, _originLongitude),
zoom: 9.4746,
);

@override
void initState() {
_addMarker(
  LatLng(_originLatitude, _originLongitude),
  "origin",
  BitmapDescriptor.defaultMarker,
);

_addMarker(
  LatLng(_destLatitude, _destLongitude),
  "destination",
  BitmapDescriptor.defaultMarkerWithHue(90),
);

_getPolyline();

 print("Enter at getpoly");
 super.initState();
 } 

@override
Widget build(BuildContext context) {
 return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'Welcome to Flutter',
  home: Scaffold(
    body: GoogleMap(
      mapType: MapType.normal,
      initialCameraPosition: _kGooglePlex,
      myLocationEnabled: true,
      tiltGesturesEnabled: true,
      compassEnabled: true,
      scrollGesturesEnabled: true,
      zoomGesturesEnabled: true,
      polylines: Set<Polyline>.of(polylines.values),
      markers: Set<Marker>.of(markers.values),
      onMapCreated: (GoogleMapController controller) {
        _controller.complete(controller);
      },
    ),
  ),
);
}

_addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
 MarkerId markerId = MarkerId(id);
 Marker marker =
    Marker(markerId: markerId, icon: descriptor, position: position);
 markers[markerId] = marker;
 }

_addPolyLine(List<LatLng> polylineCoordinates) {
 PolylineId id = PolylineId("poly");
 Polyline polyline = Polyline(
  polylineId: id,
  points: polylineCoordinates,
  width: 8,
);
polylines[id] = polyline;
setState(() {});
}

void _getPolyline() async {
List<LatLng> polylineCoordinates = [];

PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
  "API_KEY",  // My google API key
  PointLatLng(_originLatitude, _originLongitude),
  PointLatLng(_destLatitude, _destLongitude),
  travelMode: TravelMode.driving,
);
if (result.points.isNotEmpty) {
  result.points.forEach((PointLatLng point) {
    polylineCoordinates.add(LatLng(point.latitude, point.longitude));
  });
} else {
  print(result.errorMessage);
}
_addPolyLine(polylineCoordinates);
}
} 

How can I show my map with polyline?

Solution

It appears that the issue is with your API key. I run your code using my own API key and I was able to display the polyline (see screenshot below).

Please make sure that your project is linked to a valid billing account in order to use Google Maps Platform APIs. See Step 2 of "Set up your project" section of this link https://developers.google.com/maps/documentation/directions/quickstart#before_you_begin

enter image description here

Answered By – Nelson Jr.

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *