flutterdartmapsurl-schemeurl-launcher

Opening maps apps (Google Maps, Apple Maps, Waze) in Flutter with url_launcher, getting net::ERR_UNKNOWN_URL_SCHEME?


I want users to pick a maps app (Google Maps or Waze), then click on a start navigating button that opens the selected app up with pre-selected destination coordinates (latitude & longitude).

I've been using url_launcher to do this. When the button is clicked, the url opens the browser version of the app (correctly), and a prompt saying "continue in the app". When I click "continue in the app" it breaks, giving this error message: net::ERR_UNKNOWN_URL_SCHEME. This happens for Google Maps and Waze.

Here is the code called when the start navigating button is pressed:

void launchMaps(double latitude, double longitude) {
if (getSelectedMap() == "waze" && Platform.isAndroid) {
  launchUrl(Uri.parse(
      'https://waze.com/ul?ll= $latitude,$longitude&navigate=yes?utm_source=com.posttag'));
} else if (getSelectedMap() == "google" && Platform.isAndroid) {
  launchUrl(Uri.parse(
      'https://maps.google.com?saddr=My+Location&daddr=$latitude,$longitude'));
}

Thanks for any help!


Solution

  • How to a) check if a maps app (Google, Apple, Waze) is installed on the device in Flutter, and b) how to then open this app to start navigating to preselected coordinates: Check if maps apps are installed: The below code checks if maps are installed on the device. Change MapType.apple to MapType.google or MapType.waze for other maps apps.

    bool? onPhone = await MapLauncher.isMapAvailable(MapType.apple);
    

    Remember to notifyListeners(); after setting any await variables. Open the maps app to start navigating: The below code checks launches the maps apps on the device. Change MapType.apple to MapType.google or MapType.waze for other maps apps.

    await MapLauncher.showDirections(
            mapType: MapType.apple,
            destination: Coords(latitude, longitude),
            destinationTitle: address,
          );
    

    For Google Maps, special characters may need replacing, for example:

    address = address.replaceAll("&", "and");
    

    Waze doesn't allow a destinationTitle - this is displayed in other maps apps as the title of the location instead of the coordinates. The below code doesn't immediately start navigation; instead it simply just shows a marker for the destination and you can manually start navigation in-app.

    await MapLauncher.showMarker(
            mapType: MapType.apple,
            coords: Coords(latitude, longitude),
            title: address,
          );
    

    This is all done with the maps_launcher package!