How to Open Google Map App if Available With Flutter ?

· 2 min read
How to Open Map App if Available With Flutter
How to Open Map App if Available With Flutter

Now a days mobile application requires to navigate from one place to another place in that Google Map is useful. So in this article, We will learn about How to Open Google Map App if Available With Flutter?

How to Open Google Map App if Available With Flutter?

Users can open Google Map using plugin url_launcher. Consider a code snippet below:

  • Get the latest version of url_launcher and add it to pubspec.yaml
dependencies:
  flutter:
    sdk: flutter

  
  url_launcher: ^5.7.8
  • Import package like a below:
    import 'package:url_launcher/url_launcher.dart';
    class MapUtils {
    
      MapUtils._();
    
      static Future<void> openMap(double latitude, double longitude) async {
        String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
        if (await canLaunch(googleUrl)) {
          await launch(googleUrl);
        } else {
          throw 'Could not open the map.';
        }
      }
    }

    Now you can open Google Maps in your app just call this method:

    MapUtils.openMap(-3.823216,-38.481700);

    If the user doesn’t have actual lat long then the user can pass the address to Google Map like below:

  • Create a Custom Function like below:
void launchMap(String address) async {
  String query = Uri.encodeComponent(address);
  String googleUrl = "https://www.google.com/maps/search/?api=1&query=$query";

  if (await canLaunch(googleUrl)) {
    await launch(googleUrl);
  }
}
  •  Users can also try the below functions.
    static void navigateTo(double lat, double lng) async {
       var uri = Uri.parse("google.navigation:q=$lat,$lng&mode=d");
       if (await canLaunch(uri.toString())) {
          await launch(uri.toString());
       } else {
          throw 'Could not launch ${uri.toString()}';
       }
    }

Conclusion:

Hope you like this article. Do share your suggestion/feedback for the same.

In this article, we have been through How to Open Google Map App if Available With Flutter.

Thanks Again !!! Keep Learning !!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply