Search code examples
google-mapsflutterwaze

Flutter Deeplink to Google maps and Waze


I'm implementing a deeplink to a navigation app (like Google Maps or Waze) from a flutter app. I don't have problems doing so for either Google Maps or Waze, my problem is that if I configure the link for Google Maps, the user can still choose to open it in Waze, but obviously no navigation is started since parameters are different.

Is there a way to limit users choice on the app to use? Or is it possible to use a different link depending on which app the user chooses to open? Or even better, is there a universal link for navigation that works on both apps?


Solution

  • You can force to open the selected application using a Universal link (iOS) / App link (Android) and it should force the selected app to be opened. Of course, If the application you want to launch is not found in the device it will open it in the browser.

    Here I assume that you are using latitude and longitude to navigate to the place but you can easily adjust it to something else. I am also using the url_launcher to launch the links.

    void launchWaze(double lat, double lng) async {
      var url = 'waze://?ll=${lat.toString()},${lng.toString()}';
      var fallbackUrl =
          'https://waze.com/ul?ll=${lat.toString()},${lng.toString()}&navigate=yes';
      try {
        bool launched =
            await launch(url, forceSafariVC: false, forceWebView: false);
        if (!launched) {
          await launch(fallbackUrl, forceSafariVC: false, forceWebView: false);
        }
      } catch (e) {
        await launch(fallbackUrl, forceSafariVC: false, forceWebView: false);
      }
    }
    
    void launchGoogleMaps(double lat, double lng) async {
      var url = 'google.navigation:q=${lat.toString()},${lng.toString()}';
      var fallbackUrl =
          'https://www.google.com/maps/search/?api=1&query=${lat.toString()},${lng.toString()}';
      try {
        bool launched =
            await launch(url, forceSafariVC: false, forceWebView: false);
        if (!launched) {
          await launch(fallbackUrl, forceSafariVC: false, forceWebView: false);
        }
      } catch (e) {
        await launch(fallbackUrl, forceSafariVC: false, forceWebView: false);
      }
    }
    

    You also need to add the following to your Info.plist file:

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