How can apply optimization for optimize route while launch google map url?

719 Views Asked by At

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);
    }
1

There are 1 best solutions below

0
Vatsal Bhesaniya On

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);
    }