2

I want to redirect to device default google map app. So i am calling bellow url using some parameters but optimization parameter not working. So, any one have idea how apply optimization true or false while launch google map url.

This is Output Url.

String mapOptions = [  
   'origin=${widget.currentLocation.latitude.toString()}%2C${widget.currentLocation.longitude.toString()}',
      'destination=${(destinationAddress.replaceAll(',', '%2C')).replaceAll(' ', '%20')}',
      'destination_place_id=${widget.routeDeliveryDetail.deliveryList[widget.routeDeliveryDetail.deliveryList.length - 1].placeId}',
      'waypoints=$wayPoint',
      'waypoint_place_ids=$wayPointPLaceId',
      'optimization=true',
      'travelmode=driving'
    ].join('&');
    String googleUrl = 'https://www.google.com/maps/dir/?api=1&$mapOptions';
    print(googleUrl);
    try {
      if (await canLaunch(googleUrl)) {
        await launch(googleUrl);
      } else {
        throw 'Could not open the map.';
      }
    } catch (e) {
      print(e);
    }
RaSha
  • 1,356
  • 1
  • 16
  • 30
  • Your URL is wrong try this String googleUrl = 'https://www.google.com/maps/dir/api=1&$mapOptions'; Refer this Link https://medium.com/@shivani.patel18/google-maps-url-launcher-in-flutter-54cf6388b381 – Jasmin Sojitra Jun 28 '21 at 12:12

1 Answers1

0

You have to pass "optimize:true" as the first argument within the waypoints parameter to allow the Directions service to optimize the provided route by rearranging the waypoints in a more efficient order.

String mapOptions = [  
   'origin=${widget.currentLocation.latitude.toString()}%2C${widget.currentLocation.longitude.toString()}',
      'destination=${(destinationAddress.replaceAll(',', '%2C')).replaceAll(' ', '%20')}',
      'destination_place_id=${widget.routeDeliveryDetail.deliveryList[widget.routeDeliveryDetail.deliveryList.length - 1].placeId}',
      'waypoints=$wayPoint',
      'waypoint_place_ids=optimize:true|$wayPointPLaceId',
      'travelmode=driving'
    ].join('&');
    String googleUrl = 'https://www.google.com/maps/dir/?api=1&$mapOptions';
    print(googleUrl);
    try {
      if (await canLaunch(googleUrl)) {
        await launch(googleUrl);
      } else {
        throw 'Could not open the map.';
      }
    } catch (e) {
      print(e);
    }