Flutter : How to launch the Google Map app with "directions" to a predefined location?

2,226

As mentioned in the docs, you need to define the starting point lat/long and end point lat/long on the URL to prompt for a direction navigation. The URL should look similar to:

comgooglemaps://?saddr={LAT},{LONG}&daddr{LAT},{LONG}&directionsmode=driving

Share:
2,226
PeakGen
Author by

PeakGen

CTO

Updated on December 24, 2022

Comments

  • PeakGen
    PeakGen over 1 year

    in my flutter code, I am launching Google Maps app with a predefined location, on a button click. Below is the code

     _launchMaps(double lat, double lon) async {
      String googleUrl =
        'comgooglemaps://?center=${lat},${lon}';
      String appleUrl =
        'https://www.google.com/maps/search/?api=1&query=$lat,$lon';
      if (await canLaunch("comgooglemaps://")) {
        print('launching com googleUrl');
        await launch(googleUrl);
      } else if (await canLaunch(appleUrl)) {
        print('launching apple url');
        await launch(appleUrl);
      } else {
        throw 'Could not launch url';
      }
    }
    

    In iOS, I did add the following lines to the info.plist file

    <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>googlechromes</string>
            <string>comgooglemaps</string>
        </array>
    

    In Android, when I click on the button I get the google maps opened, and I can clearly see the "Directions" button where I can click and start navigating. example below

    enter image description here

    In iOS I get the Google Map opened, the location is pointed in marker, but I don't get the "Directions" button or anything like that so I can start navigating. how can I fix this?

    • jabamataro
      jabamataro over 3 years
      As per the Maps URLs documentation, you can launch request directions and launch Google Maps with the results by using the URL: https://www.google.com/maps/dir/?api=1&parameters. Have you tried this?
    • Hashem Aboonajmi
      Hashem Aboonajmi about 3 years
      @PeakGen have you achieved any result?